<?

/* UTF8 marker äöü */
class ccdsContentEditor
    extends ccdsBackendPlugin
{
    public $name="ccdsContentEditor";

    function __construct( )
    {
        parent::__construct();

        $this->xajax=ccdsXajax::getInstance();

        $this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorOpen", $this->name );
        $this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorChangeLanguage", $this->name );
        $this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentAddObj", $this->name );
        $this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorSerializeSortable", $this->name );
        $this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorListResults", $this->name );
        $this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorListAction", $this->name );
        $this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorOverview", $this->name );

        $this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorGalleryUpload", $this->name );

        $this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorReturnImage", $this->name );
        $this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorImageUpload", $this->name );

        $this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorFileUpload", $this->name );

        $this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorOverviewUpdate", $this->name );
        $this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorOverviewAction", $this->name );
        $this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorOverviewRenameObj", $this->name );
        $this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorParameter", $this->name );
        $this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorSave", $this->name );
        $this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorParameterSave", $this->name );
        $this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorVersionSave", $this->name );
        $this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorMarkObjectDeleted", $this->name );
        $this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorExpire", $this->name );
        $this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorLinkStructure", $this->name );
        $this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorCreateLinks", $this->name );
        $this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorCreateParentLinks", $this->name );
        $this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorDeleteLinks", $this->name );
        $this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorPermissionsClassesOpen", $this->name );
        $this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorPermissionsClassesSave", $this->name );
        $this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorPermissions", $this->name );
        $this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorPermissionsSave", $this->name );

        $this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorFilesUpload", $this->name );

        $this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorEdit", $this->name );
        $this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorDynamicFieldAdd", $this->name );
        $this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorDynamicTemplateNew", $this->name );

        $this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorLinks", $this->name );
    }


// -------------------------------------------------------------------------------------------------------------------

    function ContentEditorOpen( $aFormValues )
    {

        $objResponse=new ccdsResponse();

        $objResponse->openProgress();

        if ( "" == $aFormValues['project_id'] )
        {
            $query=
                "SELECT obj_key_project_id FROM obj_index WHERE obj_id='" . addslashes( $aFormValues['obj_id'] )
                . "' LIMIT 1";
            $res=$this->ccdSQL->query( $query );
            $project=$this->ccdSQL->fetch_assoc( $res );
            $aFormValues['project_id']=$project['obj_key_project_id'];
        }

        $foo=new ccdsObject( $aFormValues['obj_id'] );
        $obj=$foo->getObject( $_SESSION['CMS']['lang'][$project['obj_key_project_id']], false );

        $query=
            "SELECT * FROM classes LEFT JOIN extensions ON (class_key_ext_id = ext_id) LEFT JOIN projects ON (ext_key_project_id=project_id) WHERE class_id='"
            . $obj['obj_key_class_id'] . "' LIMIT 1";
        $res=$this->ccdSQL->query( $query );
        $row=$this->ccdSQL->fetch_assoc( $res );

        if ( file_exists( $this->config->get( 'EXTENSION_DIR' ) . $row['project_dir'] . "/" . $row['ext_dir'] . "/"
            . $row['class_dir'] . "/img/icon.png" ) )
        {
            $icon_src=$this->config->get( 'EXTENSION_WEB' ) . $row['project_dir'] . "/" . $row['ext_dir'] . "/"
                . $row['class_dir'] . "/img/icon.png";
        }
        else
        {
            $icon_src=$this->config->get( 'LIBRARY_WEB' ) . "famfamfam/icons/page.png";
        }

        $query=
            "SELECT languages.* FROM languages LEFT JOIN mn_project_language ON project_language_key_language_id=lang_id  WHERE project_language_key_project_id ='"
            . $obj['obj_key_project_id'] . "'";
        $res=$this->ccdSQL->query( $query );

        $objResponse->loadcommands( $this->tools->addTabManager( 'topTab', $obj['obj_id'],
            "<img src=\"" . $icon_src
            . "\" border=\"0\" align=\"left\" valign=\"top\" /><span style=\"margin-left:4px;\">"
            . $obj['obj_name_name'] . "</span>" ) );

        $hide_panel=array
            (
            "list" => true,
            "overview" => false,
            "edit" => true,
            "parameter" => true
            );

        $objResponse->flush();

        if ( $_SESSION['CMS']['user_flags']['content_menu_overview'] || $_SESSION['CMS']['user_level'] >= 1000 )
        {

            /* new overview panel */
            $objResponse->clsTabManager->addPanel( "TabManagerContent_" . $obj['obj_id'], array
                (
                "id" => "Coverview" . $obj['obj_id'],
                "title" => "<img src=\"" . $this->config->get( 'LIBRARY_WEB' )
                . "famfamfam/icons/application.png\" style=\"width:16px\" border=\"0\" align=\"left\" valign=\"top\" /><span style=\"margin-left:4px;\">Eigenschaften</span>",
                "content" => "<div id='ContentOverview_" . $obj['obj_id'] . "'></div>",
                "closeable" => false
                ));

            $objResponse->loadcommands( $this->ContentEditorOverview( $aFormValues ) );
            $objResponse->updateProgress( 10 );
            $objResponse->flush();

            $objResponse->clsTabManager->on( "show", "TabManagerContent_" . $obj['obj_id'], "Coverview" . $obj['obj_id'], "
				function() {
					try {
								xajax.request({ccdsfun:'ContentEditorOverview',ccdsclass:'ccdsContentEditor'},
								{
									parameters:[{
										obj_id:'" . $obj['obj_id'] . "',
										project_id:'" . $obj['obj_key_project_id'] . "'
									}]
								}
								);
					} catch(ex) {
					
					}		
				}
			" );
        }

        if ( $_SESSION['CMS']['user_flags']['content_menu_list'] || $_SESSION['CMS']['user_level'] >= 1000 )
        {
            /* new list panel */
            $objResponse->clsTabManager->addPanel( "TabManagerContent_" . $obj['obj_id'], array
                (
                "id" => "Clist" . $obj['obj_id'],
                "title" => "<img src=\"" . $this->config->get( 'LIBRARY_WEB' )
                . "famfamfam/icons/application_view_list.png\" style=\"width:16px\" border=\"0\" align=\"left\" valign=\"top\" /><span style=\"margin-left:4px;\">Liste</span>",
                "content" => "<div id='ContentList_" . $obj['obj_id'] . "'></div>",
                "hide" => $hide_panel['list'],
                "closeable" => false
                ));

            $objResponse->loadcommands( $this->ContentEditorList( $aFormValues ) );
            $objResponse->updateProgress( 30 );
            $objResponse->flush();
        }

        if ( $_SESSION['CMS']['user_flags']['content_menu_edit'] || $_SESSION['CMS']['user_level'] >= 1000 )
        {
            /* new content/edit panel */
            $objResponse->clsTabManager->addPanel( "TabManagerContent_" . $obj['obj_id'], array
                (
                "id" => "Cedit" . $obj['obj_id'],
                "title" => "<img src=\"" . $this->config->get( 'LIBRARY_WEB' )
                . "famfamfam/icons/application_form_edit.png\" style=\"width:16px\" border=\"0\" align=\"left\" valign=\"top\" /><span style=\"margin-left:4px;\">Inhalte</span>",
                "content" => "<div id='ContentEdit_" . $obj['obj_id'] . "'></div>",
                "hide" => $hide_panel['edit'],
                "closeable" => false
                ));

            $objResponse->loadcommands( $this->ContentEditorEdit( $aFormValues ) );
            $objResponse->updateProgress( 50 );
            $objResponse->flush();
        }

        if ( $_SESSION['CMS']['user_flags']['content_menu_parameter'] || $_SESSION['CMS']['user_level'] >= 1000 )
        {

            /* new parameter/settings panel */
            $objResponse->clsTabManager->addPanel( "TabManagerContent_" . $obj['obj_id'], array
                (
                "id" => "Cparameter" . $obj['obj_id'],
                "title" => "<img src=\"" . $this->config->get( 'LIBRARY_WEB' )
                . "famfamfam/icons/application_edit.png\" style=\"width:16px\" border=\"0\" align=\"left\" valign=\"top\" /><span style=\"margin-left:4px;\">Voreinstellungen</span>",
                "content" => "<div id='ContentParameter_" . $obj['obj_id'] . "'></div>",
                "hide" => $hide_panel['parameter'],
                "closeable" => false
                ));

            $objResponse->loadcommands( $this->ContentEditorParameter( $aFormValues ) );
            $objResponse->updateProgress( 60 );
            $objResponse->flush();
        }

        if ( $_SESSION['CMS']['user_flags']['content_menu_user_rights'] || $_SESSION['CMS']['user_level'] >= 1000 )
        {

            /* new permission panel */
            $objResponse->clsTabManager->addPanel( "TabManagerContent_" . $obj['obj_id'], array
                (
                "id" => "Cpermissions" . $obj['obj_id'],
                "title" => "<img src=\"" . $this->config->get( 'LIBRARY_WEB' )
                . "famfamfam/icons/group_edit.png\" style=\"width:16px\" border=\"0\" align=\"left\" valign=\"top\" /><span style=\"margin-left:4px;\">Benutzerrechte</span>",
                "content" => "<div id='ContentPermissions_" . $obj['obj_id'] . "'></div>",
                "hide" => true,
                "closeable" => false
                ));

            $objResponse->loadcommands( $this->ContentEditorPermissions( $aFormValues ) );
        }

        if ( $_SESSION['CMS']['user_flags']['content_menu_classes'] || $_SESSION['CMS']['user_level'] >= 1000 )
        {

            /* new classes panel */
            $objResponse->clsTabManager->addPanel( "TabManagerContent_" . $obj['obj_id'], array
                (
                "id" => "CpermissionClasses" . $obj['obj_id'],
                "title" => "<img src=\"" . $this->config->get( 'LIBRARY_WEB' )
                . "famfamfam/icons/package_add.png\" style=\"width:16px\" border=\"0\" align=\"left\" valign=\"top\" /><span style=\"margin-left:4px;\">Klassen</span>",
                "content" => "<div id='ContentPermissionClasses_" . $obj['obj_id'] . "'></div>",
                "hide" => true,
                "closeable" => false
                ));

            $objResponse->loadcommands( $this->ContentEditorPermissionsClassesOpen( $aFormValues ) );
        }

        if ( $_SESSION['CMS']['user_flags']['content_menu_links'] || $_SESSION['CMS']['user_level'] >= 1000 )
        {
            /* new link panel */
            $objResponse->clsTabManager->addPanel( "TabManagerContent_" . $obj['obj_id'], array
                (
                "id" => "Clinks" . $obj['obj_id'],
                "title" => "<img src=\"" . $this->config->get( 'LIBRARY_WEB' )
                . "famfamfam/icons/package_add.png\" style=\"width:16px\" border=\"0\" align=\"left\" valign=\"top\" /><span style=\"margin-left:4px;\">Links</span>",
                "content" => "<div id='ContentLinks_" . $obj['obj_id'] . "'></div>",
                "hide" => true,
                "closeable" => false
                ));

            $objResponse->loadcommands( $this->ContentEditorLinks( $aFormValues ) );
        }

        $objResponse->updateProgress( 80 );
        $objResponse->flush();

        $objResponse->clsTabManager->showPanel( "TabManagerContent_" . $obj['obj_id'], "Cedit" . $obj['obj_id'] );

				$foo = array(&$objResponse,&$aFormValues);
				$this->modules->call('ContentEditorOpen',$foo);
        $objResponse->updateProgress( 100 );
        $objResponse->flush();

        $objResponse->closeProgress();

        return $objResponse;
    }
// -------------------------------------------------------------------------------------------------------------------

// -------------------------------------------------------------------------------------------------------------------

    function ContentEditorList( $aFormValues )
    {

        $_SESSION['CMS']['ContentEditor']="list";

        $objResponse=new ccdsResponse();
        if ( "" == $aFormValues['project_id'] )
        {
            $query=
                "SELECT obj_key_project_id FROM obj_index WHERE obj_id='" . addslashes( $aFormValues['obj_id'] )
                . "' LIMIT 1";
            $res=$this->ccdSQL->query( $query );
            $project=$this->ccdSQL->fetch_assoc( $res );
            $aFormValues['project_id']=$project['obj_key_project_id'];
        }


        $foo=new ccdsObject( $aFormValues['obj_id'] );
        $obj=$foo->getObject( $_SESSION['CMS']['lang'][$aFormValues['project_id']], false );

        $this->smarty->assign( "obj", $obj );

        $query=
            "SELECT * FROM classes LEFT JOIN extensions ON (class_key_ext_id = ext_id) WHERE ext_key_project_id='"
            . $aFormValues['project_id'] . "' ORDER BY ext_name,class_name";
        $res=$this->ccdSQL->query( $query );
        $classes=array();

        while ( $row=$this->ccdSQL->fetch_assoc( $res ) )
            $classes[]=$row;

        $this->smarty->assign( "classes", $classes );

        $template=$this->tpl_dir . "content_list.tpl";

        $this->smarty->assign( "parameter", $aFormValues['parameter'] );
        $sHtml=$this->smarty->fetch( $template );

        $objResponse->assign( "ContentList_" . $obj['obj_id'], "innerHTML", $sHtml );

        if ( $obj['obj_key_tpl_backend_id'] == 0 )
            $objResponse->loadcommands( $this->ContentEditorListResults( array
                (
                "obj_id" => $aFormValues['obj_id'],
                "parameter" => array("listsearch_range" => $aFormValues['obj_id']),
                "project_id" => $aFormValues['project_id']
                )) );

        return $objResponse;
    }
// -------------------------------------------------------------------------------------------------------------------

    function ContentEditorListResults( $aFormValues )
    {
        $objResponse=new ccdsResponse();

        $foo=new ccdsObject( $aFormValues['obj_id'] );
        $obj=$foo->getObject( $_SESSION['CMS']['lang'][$aFormValues['project_id']], false );

        $this->smarty->assign( "obj", $obj );

        if ( !is_array( $aFormValues['parameter'] ) )
            $aFormValues['parameter']=array();

        if ( !isset( $aFormValues['offset'] ) )
            $aFormValues['offset']=0;
        $list_results_per_page=25;

        if ( !is_array( $aFormValues['parameter'] ) )
            $aFormValues['parameter']=array();

        if ( $aFormValues['order'] == "" )
            $aFormValues['order']="position";

        if ( $aFormValues['ordertype'] == "" )
            $aFormValues['ordertype']="asc";
        $this->smarty->assign( "parameter", $aFormValues['parameter'] );
        $this->smarty->assign( "order", $aFormValues['order'] );
        $this->smarty->assign( "offset", $aFormValues['offset'] );
        $this->smarty->assign( "ordertype", $aFormValues['ordertype'] );

        $sql_parent=
            !is_numeric( $aFormValues['parameter']['listsearch_range'] )
                ? '' : ' AND obj_parent_obj_id = ' . $aFormValues['parameter']['listsearch_range'] . ' ';
        $sql_class=
            !is_numeric( $aFormValues['parameter']['listsearch_class'] )
                ? '' : ' AND obj_key_class_id = ' . $aFormValues['parameter']['listsearch_class'] . ' ';

        $query="
           SELECT
           count(obj_id) as anzahl
           FROM
           obj_index
           LEFT OUTER JOIN
           obj_names
           ON (obj_name_key_obj_id = obj_id AND obj_name_key_lang_id='" . $_SESSION['CMS']['lang'][$obj['obj_key_project_id']] . "')
           WHERE 1
           AND obj_deleted=0
           " . $sql_parent . $sql_class;

        if ( $aFormValues['parameter']['sq'] != "" )
        {
            $query.=" AND obj_name_name LIKE '" . $aFormValues['parameter']['sq'] . "%' ";
        }
        $res=$this->ccdSQL->query( $query );
        $row=$this->ccdSQL->fetch_assoc( $res );
        $element_count=$row['anzahl'];

        $pager=array();
        $anzahl=$row['anzahl'];

        $pages=ceil( $anzahl / $list_results_per_page );
        $pagertmp=array();

        for ( $i=0; $i < $pages; $i++ )
        {
            $pagertmp[]=($i);
        }
        $pager['PAGER']=$pagertmp;
        $pager['PAGES']=$pages;

        $this->smarty->assign( "pager", $pager );

        $query=
            "
           SELECT
           *
           FROM
           obj_index
           LEFT OUTER JOIN
           obj_names
           ON (obj_name_key_obj_id = obj_id AND obj_name_key_lang_id='"
            . $_SESSION['CMS']['lang'][$obj['obj_key_project_id']] . "')
           LEFT JOIN classes ON (obj_key_class_id = class_id)
           
           WHERE 1
           AND obj_deleted=0
           "
            . $sql_parent . $sql_class;

        if ( $aFormValues['parameter']['sq'] != "" )
        {
            $query.=" AND obj_name_name LIKE '%" . $aFormValues['parameter']['sq'] . "%' ";
        }

        switch ( $aFormValues['order'] )
            {
            case "name":

                $query.="
                 ORDER BY obj_name_name " . $aFormValues['ordertype'] . "
                 ";
                break;

            case "created":

                $query.="
                 ORDER BY obj_created " . $aFormValues['ordertype'] . ", obj_name_name asc
                 ";
                break;

            case "last_edit":

                $query.="
                 ORDER BY obj_last_edit " . $aFormValues['ordertype'] . ", obj_name_name asc
                 ";
                break;

            case "position":

                $query.="
                 ORDER BY obj_position " . $aFormValues['ordertype'] . ", obj_name_name asc
                 ";
                break;

            case "class_name":

                $query.="
                 ORDER BY class_name " . $aFormValues['ordertype'] . ", obj_name_name asc
                 ";
                break;

                default:
                $query .="
                 ORDER BY obj_name_name
                 ";
                break;
            }

        $query.="
           LIMIT " . $list_results_per_page . "
           OFFSET " . ( $aFormValues['offset'] * $list_results_per_page ) . "
           ";
        $res=$this->ccdSQL->query( $query );
        $elements=array();

        while ( $row=$this->ccdSQL->fetch_assoc( $res ) )
        {
            $obj_name=
                $this->tools->getObjectName( $row['obj_id'], $_SESSION['CMS']['lang'][$obj['obj_key_project_id']],
                    true );
            $row=array_merge( $row, $obj_name );
            $elements[]=$row;
        }
        $this->smarty->assign( "elements", $elements );
        $sHtml=$this->smarty->fetch( $this->tpl_dir . "content_list_results.tpl" );
        $objResponse->assign( "DivContentEditorListResults_" . $obj['obj_id'], "innerHTML", $sHtml );

        foreach ( $elements as $elm )
        {
            $objResponse->script( "CMS.makeEditable('sortable" . $elm['obj_id'] . "');" );
        }
        return $objResponse;
    }
    

// -------------------------------------------------------------------------------------------------------------------

		function ContentEditorListAction( $aFormValues,$sortform )
		{
			$objResponse=new ccdsResponse();
		
			if ( '' != $aFormValues['obj_action'] && is_array($aFormValues['obj_list']) ) 
			{
				
				switch ($aFormValues['obj_action']) {
					case 'del':
						foreach ($aFormValues['obj_list'] as $obj_id) {
							$obj_id = (int) $obj_id;
              $this->tools->MarkObjectDeleted( $obj_id, true );
						}
						$msg = "Es wurden ".count($aFormValues['obj_list'])." Objekte gelöscht.";
						$this->tools->notify($objResponse,$msg);

						$foo = array(&$objResponse,array('project_id' => $aFormValues['project_id']));
						$this->modules->call('TrashReload',$foo);

					break;
				}
				$objResponse->loadcommands($this->ContentEditorListResults($sortform));
			}
			
			
			return $objResponse;
		}    
// -------------------------------------------------------------------------------------------------------------------

    function ContentEditorOverview( $aFormValues )
    {

        $objResponse=new ccdsResponse();

        if ( "" == $aFormValues['project_id'] )
        {
            $query=
                "SELECT obj_key_project_id FROM obj_index WHERE obj_id='" . addslashes( $aFormValues['obj_id'] )
                . "' LIMIT 1";
            $res=$this->ccdSQL->query( $query );
            $project=$this->ccdSQL->fetch_assoc( $res );
            $aFormValues['project_id']=$project['obj_key_project_id'];
        }

        $this->smarty->assign( "project_id", $aFormValues['project_id'] );

        $_SESSION['CMS']['ContentEditor']="overview";
        $obj=
            $this->tools->getObjectDetails( $aFormValues['obj_id'],
                $_SESSION['CMS']['lang'][$aFormValues['project_id']], false );
        ;
        $obj['permissions']=
            $this->tools->PermissionsObj2UserGet( $_SESSION['CMS']['user_id'], $obj['obj_id'],
                $_SESSION['CMS']['user_level'] );
        $tpl_file_org="content_overview.tpl";
					
				if (0 == $obj['obj_expire_start']) {
					$obj['obj_expire_start'] = strtotime('today 0:00');
				}

				if (0 == $obj['obj_expire_end']) {
					$obj['obj_expire_end'] = strtotime('today 23:59');
				}
					
					
        $obj['days']=array();

        for ( $i=1; $i <= 31; $i++ )
            $obj['days'][]=$i;
        $obj['months']=array();

        for ( $i=1; $i <= 12; $i++ )
            $obj['months'][]=$i;
        $this->smarty->assign( "obj", $obj );

        if ( $obj['obj_link_obj_id'] > 0 )
        {
            $query="SELECT obj_key_class_id FROM obj_index WHERE obj_id='" . $obj['obj_link_obj_id'] . "' LIMIT 1";
            $res=$this->ccdSQL->query( $query );
            $row=$this->ccdSQL->fetch_assoc( $res );
        }
        $project_languages=array();
        $query=
            "
           SELECT * FROM mn_project_language LEFT JOIN languages ON project_language_key_language_id=lang_id WHERE project_language_key_project_id='"
            . $obj['obj_key_project_id'] . "'";
        $res=$this->ccdSQL->query( $query );

        while ( $row=$this->ccdSQL->fetch_assoc( $res ) )
        {
            $project_languages[]=$row;
        }
        $this->smarty->assign( "project_languages", $project_languages );

        $obj_public=array();
        $query="
           SELECT * FROM obj_index_public WHERE objp_key_obj_id='" . $obj['obj_id'] . "'";
        $res=$this->ccdSQL->query( $query );

        while ( $row=$this->ccdSQL->fetch_assoc( $res ) )
        {
            $obj_public[$row['objp_key_lang_id']]=1;
        }
        $this->smarty->assign( "obj_public", $obj_public );

        $obj_key_class_id=$obj['obj_key_class_id'];

        $query=
            "SELECT
							*
							FROM
							classes
							LEFT JOIN
							permissions_class_2_class ON class_2_class_child_id = class_id
							
							LEFT OUTER JOIN permissions_obj_index_2_class ON
							(
								obj_index_class_key_class_id = class_2_class_child_id
								AND
								obj_index_class_key_obj_id = '"
            . $obj['obj_id'] . "'
							)
							
							WHERE
							class_2_class_node_id='" . $obj['obj_key_class_id']
                . "'
							AND (
								class_2_class_visible=1
								OR 
								obj_index_class_key_class_id IS NOT NULL
							)
							ORDER BY
							class_name
           ";

        $res=$this->ccdSQL->query( $query );
        $classes=array();

        while ( $row=$this->ccdSQL->fetch_assoc( $res ) )
        {
            $classes[]=$row;
        }
        $this->ccdSQL->free_result( $res );
        $this->smarty->assign( "classes", $classes );

        $templates=array();
        $query="SELECT templates.* FROM templates 
							LEFT JOIN mn_template_class ON (tpl_class_key_tpl_id = tpl_id)							
							WHERE 
								tpl_class_key_class_id='" . $obj['obj_key_class_id'] . "' 
							ORDER BY tpl_name";
        $res=$this->ccdSQL->query( $query );

        while ( $row=$this->ccdSQL->fetch_assoc( $res ) )
            $templates[]=$row;
        $this->ccdSQL->free_result( $res );
        $this->smarty->assign( "templates", $templates );

        $linked_objects=array();

        $query="SELECT * FROM obj_index WHERE obj_link_obj_id='" . $obj['obj_id'] . "' order by obj_position";
        $res=$this->ccdSQL->query( $query );

        while ( $row=$this->ccdSQL->fetch_assoc( $res ) )
        {
            $row=
                $this->tools->getObjectDetails( $row['obj_id'], $_SESSION['CMS']['lang'][$aFormValues['project_id']],
                    false );
            $row['PATH']=$this->tools->getRecursivePath( $row['obj_id'], false, true );
            $linked_objects[]=$row;
        }
        $linked_count=$this->ccdSQL->num_rows( $res );
        $this->ccdSQL->free_result( $res );
        $this->smarty->assign( "linked_objects", $linked_objects );
        $this->smarty->assign( "linked_count", $linked_count );

        if ( $obj['obj_parent_obj_id'] > 0 )
        {
            $query=
                "SELECT COUNT(obj_id) as anzahl FROM obj_index WHERE obj_link_obj_id = '" . $obj['obj_parent_obj_id']
                . "'";
            $res=$this->ccdSQL->query( $query );
            $row=$this->ccdSQL->fetch_assoc( $res );
            $this->ccdSQL->free_result( $res );
            $parent_link_count=$row['anzahl'];
            $this->smarty->assign( "parent_link_count", $parent_link_count );

            $query="SELECT * FROM obj_index WHERE obj_link_obj_id = '" . $obj['obj_parent_obj_id'] . "'";
            $res=$this->ccdSQL->query( $query );
            $parent_links=array();

            while ( $row=$this->ccdSQL->fetch_assoc( $res ) )
            {
                $row=
                    $this->tools->getObjectDetails( $row['obj_id'],
                        $_SESSION['CMS']['lang'][$aFormValues['project_id']], false );
                $row['PATH']=$this->tools->getRecursivePath( $row['obj_id'], false, true );

                if ( $x=$this->ccdSQL->fetch_assoc(
                    $this->ccdSQL->query( "SELECT obj_id FROM obj_index WHERE obj_parent_obj_id='"
                    . $row['obj_id'] . "' AND obj_link_obj_id='" . $aFormValues['obj_id'] . "'" ) ) )
                {
                    $row['LINKED']=1;
                }
                $parent_links[]=$row;
                usort( $parent_links, array
                    (
                    $this,
                    "sort_path"
                    ));
            }

            $this->ccdSQL->free_result( $res );
            $this->smarty->assign( "parent_links", $parent_links );
        }
        else
        {
            $parent_links=array();
            $parent_link_count=0;
            $this->smarty->assign( "parent_link_count", $parent_link_count );
            $this->smarty->assign( "parent_links", $parent_links );
        }
				
				$widgets = array();
				
				$foo = array(&$widgets);
				$this->modules->call('ContentEditor_Overview',$obj['obj_id'],$foo);
				$this->smarty->assign('widgets',$widgets);
				
        $sHtml=$this->smarty->fetch( $this->tpl_dir . $tpl_file_org );
        $objResponse->assign( "ContentOverview_" . $obj['obj_id'], "innerHTML", $sHtml );

        return $objResponse;
    }

// -------------------------------------------------------------------------------------------------------------------

    static function sort_path( $a, $b )
    {
        return strcasecmp( $a['PATH'][2]['obj_name'], $b['PATH'][2]['obj_name'] );
    }

// -------------------------------------------------------------------------------------------------------------------

    function ContentEditorLinks( $aFormValues )
    {
        $objResponse=new ccdsResponse();

        $linked_objects=array();

        $objH=new ccdsObject( $aFormValues['obj_id'] );
        $obj=$objH->getObject( $_SESSION['CMS']['lang'][$aFormValues['project_id']] );


        $this->smarty->assign( "obj", $obj );


        $query="SELECT * FROM obj_index WHERE obj_link_obj_id='" . $obj['obj_id'] . "' order by obj_position";
        $res=$this->ccdSQL->query( $query );

        while ( $row=$this->ccdSQL->fetch_assoc( $res ) )
        {
            $objH=new ccdsObject( $row['obj_id'] );
            $row=$objH->getObject( $_SESSION['CMS']['lang'][$aFormValues['project_id']], false );

            $row['PATH']=$this->tools->getRecursivePath( $row['obj_id'], false, true );
            $linked_objects[]=$row;
        }
        $linked_count=$this->ccdSQL->num_rows( $res );
        $this->ccdSQL->free_result( $res );
        $this->smarty->assign( "linked_objects", $linked_objects );
        $this->smarty->assign( "linked_count", $linked_count );

        if ( $obj['obj_parent_obj_id'] > 0 )
        {
            $query=
                "SELECT COUNT(obj_id) as anzahl FROM obj_index WHERE obj_link_obj_id = '" . $obj['obj_parent_obj_id']
                . "'";
            $res=$this->ccdSQL->query( $query );
            $row=$this->ccdSQL->fetch_assoc( $res );
            $this->ccdSQL->free_result( $res );
            $parent_link_count=$row['anzahl'];
            $this->smarty->assign( "parent_link_count", $parent_link_count );

            $query="SELECT * FROM obj_index WHERE obj_link_obj_id = '" . $obj['obj_parent_obj_id'] . "'";
            $res=$this->ccdSQL->query( $query );
            $parent_links=array();

            while ( $row=$this->ccdSQL->fetch_assoc( $res ) )
            {

                $objH=new ccdsObject( $row['obj_id'] );
                $row=$objH->getObject( $_SESSION['CMS']['lang'][$aFormValues['project_id']] );
                $row['PATH']=$this->tools->getRecursivePath( $row['obj_id'], false, true );

                if ( $x=$this->ccdSQL->fetch_assoc(
                    $this->ccdSQL->query( "SELECT obj_id FROM obj_index WHERE obj_parent_obj_id='"
                    . $row['obj_id'] . "' AND obj_link_obj_id='" . $aFormValues['obj_id'] . "'" ) ) )
                {
                    $row['LINKED']=1;
                }
                $parent_links[]=$row;
                usort( $parent_links, array
                    (
                    $this,
                    "sort_path"
                    ));
            }

            $this->ccdSQL->free_result( $res );
            $this->smarty->assign( "parent_links", $parent_links );
        }
        else
        {
            $parent_links=array();
            $parent_link_count=0;
            $this->smarty->assign( "parent_link_count", $parent_link_count );
            $this->smarty->assign( "parent_links", $parent_links );
        }
				

				$contentarray = array();
				$foo = array(&$contentarray);
				
				$this->modules->call('ccdsContentEditorLinks_tpl',$foo);
				$content = implode('',$contentarray);

				$this->smarty->assign('contentarray',$contentarray);

				$this->smarty->assign('plugin_content',$content);

        $sHtml=$this->smarty->fetch( $this->tpl_dir . "content_links.tpl" );
        $objResponse->assign( "ContentLinks_" . $aFormValues['obj_id'], "innerHTML", $sHtml );
        $objResponse->loadcommands( $this->ContentEditorLinkStructure( array
            (
            "obj_id" => 0,
            "current_obj_id" => $aFormValues['obj_id'],
            'project_id' => $aFormValues['project_id']
            )) );

        return $objResponse;
    }

// -------------------------------------------------------------------------------------------------------------------

    function ContentEditorLinkStructure( $aFormValues )
    {

        $objResponse=new ccdsResponse();

        $query=
            "SELECT obj_id FROM obj_index WHERE obj_parent_obj_id='" . $aFormValues['obj_id']
            . "' AND obj_deleted=0 ORDER BY obj_position";
        $res=$this->ccdSQL->query( $query );
        $structure=array();
//
        while ( $row=$this->ccdSQL->fetch_assoc( $res ) )
        {
						$foo = new ccdsObject($row['obj_id']);
            $structure[]=$foo->getObject($_SESSION['CMS']['lang'][$aFormValues['project_id']],false,true);
        }
        $this->ccdSQL->free_result( $res );
        $this->smarty->assign( "current_obj_id", $aFormValues['current_obj_id'] );
        $this->smarty->assign( "structure", $structure );
        $this->smarty->assign( "project_id", $aFormValues['project_id'] );
        $sHtml=$this->smarty->fetch( $this->tpl_dir . "content_structure_listing.tpl" );
        $objResponse->assign( "linkstructure_obj" . $aFormValues['obj_id'] . '_' . $aFormValues['current_obj_id'],
            "innerHTML", $sHtml );

        return $objResponse;
    }
// -------------------------------------------------------------------------------------------------------------------

    function ContentEditorDeleteLinks( $aFormValues )
    {

        $objResponse=new ccdsResponse();
        

        if ( is_array( $aFormValues['dellink'] ) ) {
            foreach ( $aFormValues['dellink'] as $link )
            {
              $this->ccdSQL->query( "DELETE FROM obj_index WHERE obj_id='" . $link . "' LIMIT 1" );
			     		$this->ccdSQL->query("DELETE FROM obj_index_public WHERE objp_key_obj_id='".$link."'");
            }
        } 
        $objResponse->loadcommands( $this->ContentEditorLinks( $aFormValues ) );
        return $objResponse;
    }


// -------------------------------------------------------------------------------------------------------------------
    function ContentEditorCreateParentLinks( $aFormValues )
    {
        $objResponse=new ccdsResponse();

				$query = "SELECT obj_parent_obj_id,obj_key_project_id FROM obj_index WHERE obj_id='".addslashes($aFormValues['obj_id'])."' LIMIT 1";
				$res = $this->ccdSQL->query($query);
				$obj = $this->ccdSQL->fetch_assoc($res);
//
        if ( $aFormValues['link_parents'] == 1 )
        {
            
            $query="SELECT obj_id FROM obj_index WHERE obj_link_obj_id='" . $obj['obj_parent_obj_id'] . "'";
            $res=$this->ccdSQL->query( $query );
            while ( $row=$this->ccdSQL->fetch_assoc( $res ) )
            {
                $this->CreateLinkObject( (int) $aFormValues['obj_id'], $row['obj_id'], true	 );
            }
        }
        $objResponse->loadcommands( $this->ContentEditorLinks( $aFormValues) );
        return $objResponse;
    }

// -------------------------------------------------------------------------------------------------------------------

    function CreateLinkObject( $source_obj_id, $dest_obj_id, $recursive=false )
    {



        $query=
            "SELECT * FROM obj_index WHERE obj_link_obj_id='" . $source_obj_id . "' AND obj_parent_obj_id = '"
            . $dest_obj_id . "' LIMIT 1";
        $res=$this->ccdSQL->query( $query );

        if ( !$row=$this->ccdSQL->fetch_assoc( $res ) )
        {


		        $query="SELECT * FROM obj_index WHERE obj_id='" . $source_obj_id . "'";
		        $res=$this->ccdSQL->query( $query );
		        $source_row=$this->ccdSQL->fetch_assoc( $res );
		
            $obj=new ccdsObject( false, array
                (
                "obj_parent_obj_id" => $dest_obj_id,
                "obj_key_tpl_id" => $source_row['obj_key_tpl_id'],
                "user_id" => $_SESSION['CMS']['user_id'],
                "obj_cacheable" => $source_row['obj_cacheable'],
                "obj_is_navi" => $source_row['obj_is_navi'],
                "class_id" => $source_row['obj_key_class_id'],
                "project_id" => $source_row['obj_key_project_id'],
                "obj_link_obj_id" => $source_obj_id,
                "obj_expire" => $source_row['obj_expire'],
                "obj_expire_start" => $source_row['obj_expire_start'],
                "obj_expire_end" => $source_row['obj_expire_end'],
                "obj_expire_del" => $source_row['obj_expire_del'],
                "obj_no_structure" => $source_row['obj_no_structure']
                ));

            $new_id=$obj->getObjId();
            $query="SELECT * FROM obj_index_public WHERE objp_key_obj_id='" . $source_row['obj_id'] . "'";
            $res=$this->ccdSQL->query( $query );

            while ( $row=$this->ccdSQL->fetch_assoc( $res ) )
            {
                $this->ccdSQL->query( "INSERT IGNORE INTO obj_index_public VALUES('" . $new_id . "','" . $row['objp_key_lang_id']
                    . "')" );
            }
        }
        else
        {
            $new_id=$row['obj_id'];
        }

        if ( $recursive )
        {

            $query=
                "SELECT obj_id FROM obj_index WHERE obj_parent_obj_id='" . $source_obj_id
                . "' AND obj_deleted=0 AND obj_link_obj_id=0";
            $res=$this->ccdSQL->query( $query );

            while ( $row=$this->ccdSQL->fetch_assoc( $res ) )
            {
                $this->CreateLinkObject( $row['obj_id'], $new_id, true );
            }
        }

    }

// -------------------------------------------------------------------------------------------------------------------

    function ContentEditorCreateLinks( $aFormValues )
    {

        $objResponse=new ccdsResponse();

        if ( ( $aFormValues['link_current_obj'] == 1 ) OR ( $aFormValues['link_below'] == 1 ) )
        {
            if ( is_array( $aFormValues['link_into'] ) )
            {
                foreach ( $aFormValues['link_into'] as $dest_obj_id )
                {
                    if ( $aFormValues['link_current_obj'] == 1 )
                    {
                        $aFormValues['link_below'] == 1 ? $recursive=true : $recursive=false;
                        $this->CreateLinkObject( $aFormValues['obj_id'], $dest_obj_id, $recursive );
                    }
                    else
                    {
                        $query=
                            "SELECT * FROM obj_index WHERE obj_parent_obj_id='" . $aFormValues['obj_id']
                            . "' AND obj_deleted=0 AND obj_link_obj_id=0";
                        $res=$this->ccdSQL->query( $query );

                        while ( $row=$this->ccdSQL->fetch_assoc( $res ) )
                        {
                            $this->CreateLinkObject( $row['obj_id'], $dest_obj_id, true );
                        }
                    }
                }
            }
        }
        $objResponse->loadcommands( $this->ContentEditorLinks( array("obj_id" => $aFormValues['obj_id'])) );
        return $objResponse;
    }

// -------------------------------------------------------------------------------------------------------------------

    function ContentEditorSerializeSortable( $aFormValues )
    {

        $objResponse=new ccdsResponse();
        $obj_id=str_replace( "sortable", "", $aFormValues['obj_id'] );

        $query="SELECT * FROM obj_index WHERE obj_id='" . $obj_id . "'";
        $res=$this->ccdSQL->query( $query );
        $obj=$this->ccdSQL->fetch_assoc( $res );

        if ( $obj['obj_position'] < $aFormValues['position'] )
        {
            $this->ccdSQL->query( "UPDATE obj_index SET obj_position=obj_position-1 WHERE obj_position >"
                . $obj['obj_position'] . " AND obj_position <= " . $aFormValues['position'] . " AND obj_parent_obj_id='"
                . $obj['obj_parent_obj_id'] . "'" );
            $this->ccdSQL->query( "UPDATE obj_index SET obj_position='" . $aFormValues['position'] . "' WHERE obj_id='"
                . $obj_id . "'" );
        }
        elseif ( $obj['obj_position'] > $aFormValues['position'] )
        {
            $this->ccdSQL->query( "UPDATE obj_index SET obj_position=obj_position+1 WHERE obj_position >= "
                . $aFormValues['position'] . " AND obj_position < " . $obj['obj_position'] . " AND obj_parent_obj_id='"
                . $obj['obj_parent_obj_id'] . "'" );
            $this->ccdSQL->query( "UPDATE obj_index SET obj_position='" . $aFormValues['position'] . "' WHERE obj_id='"
                . $obj_id . "'" );
        }

        $query=
            "SELECT obj_id FROM obj_index WHERE obj_parent_obj_id = '" . addslashes( $obj['obj_parent_obj_id'] )
            . "' ORDER BY obj_position";
        $res=$this->ccdSQL->query( $query );

        while ( $row=$this->ccdSQL->fetch_assoc( $res ) )
        {
            $new_positions[]=$row['obj_id'];
        }

        if ( 0 < $obj_id )
        {
            $query="SELECT obj_parent_obj_id FROM obj_index WHERE obj_link_obj_id = '" . $obj_id . "' ";
            $res=$this->ccdSQL->query( $query );

            while ( $row=$this->ccdSQL->fetch_assoc( $res ) )
            {
                $foo=$this->sortLinkedObjects( $row['obj_parent_obj_id'], $new_positions );
            }
        }

        $objResponse->script( "editComplete('" . $aFormValues['position'] . "','" . $aFormValues['obj_id'] . "')" );
        $objResponse->loadClassCommand( "ccdsDefaultPlugin", "updateNaviEntry", array
            (
            "project_id" => $obj['obj_key_project_id'],
            "obj_parent_obj_id" => $obj['obj_parent_obj_id']
            ), true );

        $objResponse->loadCommands( $this->ContentEditorList( array("obj_id" => $obj['obj_parent_obj_id'])) );

        return $objResponse;
    }

// -------------------------------------------------------------------------------------------------------------------

    function sort_array( &$orgarray, &$sortarray )
    {
        $new=array();
        $tmp=array();
        $not_found=array();

        foreach ( $sortarray as $key => $value )
        {
            $pos=array_search( (string)$value, $orgarray );

            if ( false === $pos )
            {
                $not_found[$key]=$value;
            }
            else
            {
                $tmp[$pos]=$value;
            }
        }
        $diff=array_diff( $sortarray, $tmp );
        ksort ($tmp);

        foreach ( $diff as $key => $value )
        {
            array_insert( $tmp, $key, $value );
        }
        return $tmp;
    }


// -------------------------------------------------------------------------------------------------------------------
    function sortLinkedObjects( $obj_parent_obj_id, $new_positions )
    {
        $query="SELECT * FROM obj_index WHERE obj_parent_obj_id='" . $obj_parent_obj_id . "'  ORDER BY obj_position";
        $res=$this->ccdSQL->query( $query );
        $i=1;
        $current_positions=array();
        $ids=array();

        while ( $row=$this->ccdSQL->fetch_assoc( $res ) )
        {
            $pos=$row['obj_link_obj_id'] > 0 ? $row['obj_link_obj_id'] : $row['obj_id'];
            $current_positions[]=$pos;
            $ids[$pos]=$row['obj_id'];
        }
        $this->sort_array( $new_positions, $current_positions );
        $i=1;

        foreach ( $new_positions as $obj_id )
        {
            $query=
                "UPDATE obj_index SET obj_position = '" . $i . "' WHERE obj_id = '" . $ids[$obj_id]
                . "' AND obj_parent_obj_id='" . $obj_parent_obj_id . "' LIMIT 1";
            $this->ccdSQL->query( $query );
            $i++;
        }
    }

// -------------------------------------------------------------------------------------------------------------------
    function ContentAddObj( $aFormValues )
    {

        $objResponse=new ccdsResponse();

        if ( $aFormValues['obj_key_class_id'] == "" )
        {
            $objResponse->alert( "Keine Klasse gewählt!" );
            return $objResponse;
        }
        else
        {
            if ( "" == $aFormValues['project_id'] )
            {
                $query=
                    "SELECT obj_key_project_id FROM obj_index WHERE obj_id='"
                    . addslashes( $aFormValues['obj_parent_obj_id'] ) . "' LIMIT 1";
                $res=$this->ccdSQL->query( $query );
                $project=$this->ccdSQL->fetch_assoc( $res );
                $aFormValues['project_id']=$project['obj_key_project_id'];
            }

            $obj=new ccdsObject( false, array
                (
                "obj_parent_obj_id" => $aFormValues['obj_parent_obj_id'],
                "class_id" => $aFormValues['obj_key_class_id'],
                "obj_name" => $aFormValues['obj_name'],
                "lang_id" => $_SESSION['CMS']['lang'][$aFormValues['project_id']],
                "user_id" => $_SESSION['CMS']['user_id'],
                "obj_is_navi" => false,
                "project_id" => $aFormValues['project_id']
                ));

            if ( 0 < $aFormValues['obj_parent_obj_id'] )
            {
                $objResponse->loadClassCommand( "ccdsDefaultPlugin", "updateNaviEntry", array
                    (
                    "project_id" => $aFormValues['project_id'],
                    "obj_parent_obj_id" => $aFormValues['obj_parent_obj_id']
                    ), true );
            }
            else
            {
                $objResponse->loadClassCommand( "ccdsDefaultPlugin", "initNavi" );
            }

            if ( 1 == $aFormValues['first_pos'] )
                $objResponse->loadcommands( $this->ContentEditorSerializeSortable( array
                    (
                    'obj_id' => $obj->getObjId(),
                    'position' => 1
                    )) );

            if ( 1 == $aFormValues['jump_to_obj'] )
                $objResponse->loadcommands( $this->ContentEditorOpen( array("obj_id" => $obj->getObjId())) );
					
            $_SESSION['CMS']['last_inserted_class']=$aFormValues['obj_key_class_id'];
            $aFormValues['jump_to_obj']=1 == $aFormValues['jump_to_obj'] ? true : false;
            $_SESSION['CMS']['jump_to_obj']=$aFormValues['jump_to_obj'];
        	
        }

       	$this->tools->notify($objResponse,"Das Objekt \"".$aFormValues['obj_name']."\" wurde angelegt.");
				$objResponse->script("jQuery('#contentAddObjectWindow').xWindow('destroy')");
        return $objResponse;
    }

// -------------------------------------------------------------------------------------------------------------------

    function ContentEditorOverviewRenameObj( $aFormValues )
    {

        $objResponse=new ccdsResponse();

        if ( "" == $aFormValues['project_id'] )
        {
            $query=
                "SELECT obj_key_project_id,obj_parent_obj_id FROM obj_index WHERE obj_id='"
                . addslashes( $aFormValues['obj_id'] ) . "' LIMIT 1";
            $res=$this->ccdSQL->query( $query );
            $project=$this->ccdSQL->fetch_assoc( $res );
            $aFormValues['project_id']=$project['obj_key_project_id'];
        }


	      $this->tools->log($_SESSION['CMS']['user_id'],$aFormValues['obj_id'],'Update Eigenschaft (name,is_navi,vorlage)');


        $obj=
            $this->tools->getObjectDetails( $aFormValues['obj_id'],
                $_SESSION['CMS']['lang'][$aFormValues['project_id']], false );

        $query=
            "SELECT * FROM obj_names WHERE obj_name_key_obj_id='" . $aFormValues['obj_id']
            . "' AND obj_name_key_lang_id='" . $_SESSION['CMS']['lang'][$aFormValues['project_id']] . "'";
        $res=$this->ccdSQL->query( $query );

        if ( $row=$this->ccdSQL->fetch_assoc( $res ) )
        {
            $this->ccdSQL->query( "UPDATE
                   obj_names
                   SET obj_name_name='" . addslashes( $aFormValues['obj_name_name'] ) . "',
                   obj_name_serialized ='" . $this->tools->serialize_name( $aFormValues['obj_name_name'], $obj['obj_parent_obj_id'], $_SESSION['CMS']['lang'][$aFormValues['project_id']], $aFormValues['obj_id'] ) . "'
                   WHERE obj_name_id='" . $row['obj_name_id'] . "'" );
        }
        else
        {
            $query=" INSERT INTO obj_names
               (
               obj_name_name,
               obj_name_key_obj_id,
               obj_name_key_lang_id,
               obj_name_serialized
               )
               VALUES (
               '" . addslashes( $aFormValues['obj_name_name'] ) . "',
               '" . $aFormValues['obj_id'] . "',
               '" . $_SESSION['CMS']['lang'][$aFormValues['project_id']] . "',
               '" . $this->tools->serialize_name( $aFormValues['obj_name_name'], $obj['obj_parent_obj_id'], $_SESSION['CMS']['lang'][$aFormValues['project_id']], $aFormValues['obj_id'] ) . "'
               )
               ";
            $this->ccdSQL->query( $query );
        }

        if ( $aFormValues['obj_is_cacheable'] == "" )
        {
            $aFormValues['obj_is_cacheable']=0;
        }

        if ( $aFormValues['obj_is_navi'] == "" )
        {
            $aFormValues['obj_is_navi']=0;
        }

        if ( $aFormValues['obj_no_elements'] == "" )
        {
            $aFormValues['obj_no_elements']=0;
        }
        if ( $aFormValues['obj_preview'] == "" )
        {
            $aFormValues['obj_preview']=0;
        }

        $this->ccdSQL->query( "UPDATE obj_index SET
               obj_cacheable='" . (int) $aFormValues['obj_cacheable'] . "',
               obj_is_navi='" . (int) $aFormValues['obj_is_navi'] . "', 
               obj_last_edit='" . time() . "',
               obj_no_elements='" . (int) $aFormValues['obj_no_elements'] . "', 
               obj_last_edit_key_user_id='" . $_SESSION['CMS']['user_id'] . "',
               obj_key_tpl_id='" . (int) $aFormValues['obj_key_tpl_id'] . "',
               obj_preview='" . (int) $aFormValues['obj_preview'] . "' 
               WHERE
               obj_id='" . $aFormValues['obj_id'] . "' " );

        if ( $aFormValues['apply_linked'] == 1 )
        {
            $query="SELECT obj_id FROM obj_index WHERE obj_link_obj_id='" . $aFormValues['obj_id'] . "'";
            $res=$this->ccdSQL->query( $query );

            while ( $row=$this->ccdSQL->fetch_assoc( $res ) )
            {
                $this->ccdSQL->query( "UPDATE obj_index SET obj_cacheable='" . $aFormValues['obj_cacheable'] . "',	
									obj_is_navi='" . $aFormValues['obj_is_navi'] . "', 
									obj_last_edit='" . time() . "',
									obj_no_elements='" . (int) $aFormValues['obj_no_elements'] . "', 
									obj_last_edit_key_user_id='" . $_SESSION['CMS']['user_id'] . "',
									obj_key_tpl_id='" . $aFormValues['obj_key_tpl_id'] . "',
		              obj_preview='" . (int) $aFormValues['obj_preview'] . "' 
									WHERE
									obj_id='" . $row['obj_id'] . "' " );
            }
        }

        $query=
            "SELECT * FROM classes LEFT JOIN extensions ON (class_key_ext_id = ext_id) LEFT JOIN projects ON (ext_key_project_id=project_id) WHERE class_id='"
            . $obj['obj_key_class_id'] . "' LIMIT 1";
        $res=$this->ccdSQL->query( $query );
        $row=$this->ccdSQL->fetch_assoc( $res );

        if ( file_exists( $this->config->get( 'EXTENSION_DIR' ) . $row['project_dir'] . "/" . $row['ext_dir'] . "/"
            . $row['class_dir'] . "/img/icon.png" ) )
        {
            $icon_src=$this->config->get( 'EXTENSION_WEB' ) . $row['project_dir'] . "/" . $row['ext_dir'] . "/"
                . $row['class_dir'] . "/img/icon.png";
        }
        else
        {
            $icon_src=$this->config->get( 'LIBRARY_WEB' ) . "famfamfam/icons/page.png";
        }

        $objResponse->clsTabManager->setTitle( 'topTab', 'tab_' . $aFormValues['obj_id'],
            "<img src=\"" . $icon_src
            . "\" border=\"0\" align=\"left\" valign=\"top\" /><span style=\"margin-left:4px;\">"
            . $aFormValues['obj_name_name'] . "</span>" );
        $objResponse->loadClassCommand( "ccdsDefaultPlugin", "updateNaviEntry", array
            (
            "project_id" => $aFormValues['project_id'],
            "obj_parent_obj_id" => $aFormValues['obj_id']
            ));

       	$this->tools->notify($objResponse,"Die Einstellungen wurden gespeichert.");

        return $objResponse;
    }


// -------------------------------------------------------------------------------------------------------------------

    private function ContentEditorEditEvents( &$objResponse, &$FormField, &$obj )
    {

        $tab_id="TabManagerContent_" . $obj['obj_id'];
        $panel_id="Cedit" . $obj['obj_id'];

        switch ( $FormField['formtype'] )
            {
            case 'date':
            	$objResponse->script("
try {
            	$('#obj_".$obj['obj_id']."_".$FormField['fieldname']."_date').datepicker($.extend({}, 
								$.datepicker.regional['de'], { 
								    showStatus: true, 
								    showOn: 'button', 
								    buttonImage: '/ccds_library/famfamfam/icons/calendar.png', 
								    buttonImageOnly: true 
							}));
} catch(ex) {
	console.log(ex);
}
            	");
            
            
            
            break;
            	
            case 'html':

                $tiny_id="obj_" . $obj['obj_id'] . "_" . $FormField['fieldname'];
                $objResponse->script( "
            tinyMCE.execCommand('mceAddControl', false, '" . $tiny_id . "');
            " );
                //	            	xajax.ext.tabManager.showPanel('".$tab_id."','".$panel_id."');

                $objResponse->clsTabManager->on( 'destroy', $tab_id, $panel_id, "function() {
	            	tinyMCE.execCommand('mceFocus', false, '" . $tiny_id . "');
	            	tinyMCE.execCommand('mceRemoveControl', false, '" . $tiny_id . "');
								}" );

                break;

            case 'file':
                if ( $FormField['default_path'] != "" ||  ($_SESSION['CMS']['user_dir'] != "" && !$_SESSION['CMS']['user_flags']['general_filemanager']) )
                {
                    $objResponse->clsSwfUpload->transField( "up_" . $obj['obj_id'] . "_" . $FormField['fieldname'],
                        array
                        (
                        "file_types_description" => "Alle Dateien",
                        "file_size_limit" => "3 MB",
                        "file_types" => "*.*",
                        "file_queue_limit" => 1,
                        "post_params" => array
                            (
                            "PHPSESSID" => session_id(),
                            "path" => $FormField['default_path'],
                            "root" => "media"
                            )
                        ), false );

                    $objResponse->clsTabManager->on( 'destroy', "TabManagerContent_" . $obj['obj_id'], "Cedit" . $obj['obj_id'], "function() {
											xajax.ext.SWFupload.tools.destroyField('" . $FormField['fieldname'] . "_" . $obj['obj_id'] . "_up" . "');
								}" );
                }
                break;

            case 'image':
                if ( '' != $FormField['default_path'] ||  ($_SESSION['CMS']['user_dir'] != "" && !$_SESSION['CMS']['user_flags']['general_filemanager']))
                {
                    $objResponse->clsSwfUpload->transField( "up_" . $obj['obj_id'] . "_" . $FormField['fieldname'],
                        array
                        (
                        "file_types_description" => "Bilder",
                        "file_size_limit" => $this->tools->return_upload_limit()." B",
                        "file_types" => "*.jpg;*.gif;*.png;*.tiff;*.jpeg",
                        "file_queue_limit" => 1,
                        "post_params" => array
                            (
                            "PHPSESSID" => session_id(),
                            "path" => $FormField['default_path'],
                            "root" => "media"
                            )
                        ), false );

                    $objResponse->clsTabManager->on( 'destroy', "TabManagerContent_" . $obj['obj_id'], "Cedit" . $obj['obj_id'], "function() {
											xajax.ext.SWFupload.tools.destroyField('" . $FormField['fieldname'] . "_" . $obj['obj_id'] . "_up" . "');
								}" );
                }
                break;

            case 'gallery':
                if ( isset( $FormField['default_path'] ) ||  ($_SESSION['CMS']['user_dir'] != "" && !$_SESSION['CMS']['user_flags']['general_filemanager']))
                {
                    $objResponse->clsSwfUpload->transField( $FormField['fieldname'] . "_" . $obj['obj_id'] . "_multiup",
                        array
                        (
                        "file_types_description" => "Bilder",
                        "file_size_limit" => $this->tools->return_upload_limit()." B",
                        "file_types" => "*.jpg;*.gif;*.png;*.tiff;*.jpeg",
                        "post_params" => array
                            (
                            "PHPSESSID" => session_id(),
                            "path" => $FormField['default_path'],
                            "root" => "media"
                            )
                        ), true );

                    $objResponse->clsTabManager->on( 'destroy', "TabManagerContent_" . $obj['obj_id'], "Cedit" . $obj['obj_id'], "function() {
											xajax.ext.SWFupload.tools.destroyField('" . $FormField['fieldname'] . "_" . $obj['obj_id'] . "_multiup" . "');
										}" );
                }
                $objResponse->script( "
						$('#GallerySortable_" . $obj['obj_vers_id'] . '_' . $FormField['fieldname'] . "').sortable({});
					" );
                break;

            case 'files':
                if ( '' != $FormField['default_path'] ||  ($_SESSION['CMS']['user_dir'] != "" && !$_SESSION['CMS']['user_flags']['general_filemanager']))
                {
                    $objResponse->clsSwfUpload->transField( $FormField['fieldname'] . "_" . $obj['obj_id'] . "_multiup",
                        array
                        (
                        "file_types_description" => "",
                        "file_size_limit" => $this->tools->return_upload_limit()." B",
                        "file_types" => "*.*",
                        "post_params" => array
                            (
                            "PHPSESSID" => session_id(),
                            "path" => $FormField['default_path'],
                            "root" => "media"
                            )
                        ), true );

                    $objResponse->clsTabManager->on( 'destroy', $tab_id, $panel_id, "function() {
											xajax.ext.SWFupload.tools.destroyField('" . $FormField['fieldname'] . "_" . $obj['obj_id'] . "_multiup" . "');
										}" );
                }
                $objResponse->script( "
						$('#FilesSortable_" . $obj['obj_vers_id'] . '_' . $FormField['fieldname'] . "').sortable({});
					" );
                break;

            default:

            break;
            }
    }


// -------------------------------------------------------------------------------------------------------------------

    function ContentEditorEdit( $aFormValues )
    {
        $objResponse=new ccdsResponse();

        $_SESSION['CMS']['ContentEditor']="edit";
        

        if ( "" == $aFormValues['project_id'] )
        {

            $query=
                "SELECT obj_key_project_id FROM obj_index WHERE obj_id='" . addslashes( $aFormValues['obj_id'] )
                . "' LIMIT 1";
            $res=$this->ccdSQL->query( $query );
            $project=$this->ccdSQL->fetch_assoc( $res );
            $aFormValues['project_id']=$project['obj_key_project_id'];
        }

        if (true === $aFormValues['reload']) {
            $objResponse->clsTabManager->addPanel( "TabManagerContent_" . $aFormValues['obj_id'], array
                (
                "id" => "Cedit" . $aFormValues['obj_id'],
                "title" => "<img src=\"" . $this->config->get( 'LIBRARY_WEB' )
                . "famfamfam/icons/application_form_edit.png\" style=\"width:16px\" border=\"0\" align=\"left\" valign=\"top\" /><span style=\"margin-left:4px;\">Inhalte</span>",
                "content" => "<div id='ContentEdit_" . $aFormValues['obj_id'] . "'></div>",
                "closeable" => false
                ));        	
        }



        $this->smarty->assign( 'project_id', $aFormValues['project_id'] );

        $query=
            "SELECT * FROM obj_index LEFT OUTER JOIN obj_names ON (obj_name_key_obj_id = obj_id AND obj_name_key_lang_id='"
            . $_SESSION['CMS']['lang'][$aFormValues['project_id']]
                . "')LEFT OUTER JOIN obj_versions_index ON obj_vers_key_obj_id = obj_id WHERE obj_id='"
            . $aFormValues['obj_id'] . "'";
        $res=$this->ccdSQL->query( $query );
        $obj=$this->ccdSQL->fetch_assoc( $res );
        $obj['permissions']=
            $this->tools->PermissionsObj2UserGet( $_SESSION['CMS']['user_id'], $obj['obj_id'],
                $_SESSION['CMS']['user_level'] );
        $this->smarty->assign( "obj", $obj );

        $edit_obj_id=0 < $obj['obj_link_obj_id'] ? $obj['obj_link_obj_id'] : $obj['obj_id'];

        if ( $aFormValues['obj_vers_id'] == "" )
        {
            $query="SELECT * FROM obj_versions_index WHERE obj_vers_key_obj_id='" . $edit_obj_id
                . "' AND obj_vers_key_lang_id='" . $_SESSION['CMS']['lang'][$aFormValues['project_id']]
                . "' ORDER BY obj_vers_created desc LIMIT 1";
        }
        else
        {
            $query="SELECT * FROM obj_versions_index WHERE obj_vers_id='" . $aFormValues['obj_vers_id']
                . "' AND obj_vers_key_lang_id='" . $_SESSION['CMS']['lang'][$aFormValues['project_id']]
                . "' ORDER BY obj_vers_created desc LIMIT 1";
        }

        $res=$this->ccdSQL->query( $query );

        if ( $row=$this->ccdSQL->fetch_assoc( $res ) )
        {
            $obj_vers_id=$row['obj_vers_id'];
        }
        else
        {
            $obj_vers_id=false;
        }

        $this->smarty->assign( "obj_vers_id", $obj_vers_id );
        $foo=new ccdsObject( $edit_obj_id );
				
        if ( $foo->hasContent() )
					$aFormFields=$foo->getEditArray( $obj_vers_id, $_SESSION['CMS']['lang'][$aFormValues['project_id']] );


        $this->smarty->assign( "aFormFields", $aFormFields );

        if ( $foo->version_control() )
        {

            $obj_versions_index=array();
            $query=
                "SELECT * FROM obj_versions_index LEFT OUTER JOIN user ON obj_vers_creator = user_id
	           WHERE obj_vers_key_obj_id='" . $aFormValues['obj_id'] . "' AND obj_vers_key_lang_id = '"
                . $_SESSION['CMS']['lang'][$aFormValues['project_id']] . "' ORDER BY obj_vers_created desc LIMIT 10";
            $res=$this->ccdSQL->query( $query );

            while ( $row=$this->ccdSQL->fetch_assoc( $res ) )
            {
                $obj_versions_index[]=$row;
            }

            $this->smarty->assign( "obj_versions", $obj_versions_index );
        }
        else
        {
            $this->smarty->assign( "obj_versions", false );
        }

        $query=
            "SELECT languages.* FROM languages LEFT JOIN mn_project_language ON project_language_key_language_id=lang_id  WHERE project_language_key_project_id ='"
            . $obj['obj_key_project_id'] . "'";
        $res=$this->ccdSQL->query( $query );

				
				$save_templates=array();
				$foo = array(&$save_templates);
				$this->modules->call('ccdsContentEditorSaveTemplates',$foo);
				$this->smarty->assign('save_templates',$save_templates);

        $sHtml=$this->smarty->fetch( $this->tpl_dir . "content_edit.tpl" );
        $objResponse->assign( "ContentEdit_" . $aFormValues['obj_id'], "innerHTML", $sHtml );
        $destroy_callback="";

        $foo="";

        if ( is_array( $aFormFields ) )
        {
            foreach ( $aFormFields as $FormField )
            {
                $this->ContentEditorEditEvents( $objResponse, $FormField, $obj );

                if ( "fieldset" == $FormField['formtype'] )
                {
                    if ( !is_array( $FormField['fields'] ) )
                        $FormField['fields']=array();

                    foreach ( $FormField['fields'] as $Field )
                    {
                        $this->ContentEditorEditEvents( $objResponse, $Field, $obj );
                    }
                }
            }
        }
        return $objResponse;
    }

// -------------------------------------------------------------------------------------------------------------------

    function ContentEditorGalleryUpload( $aFormValues )
    {

        $objResponse=new ccdsResponse();

        if ( "" == $aFormValues['project_id'] )
        {
            $query=
                "SELECT obj_key_project_id FROM obj_index WHERE obj_id='" . addslashes( $aFormValues['obj_id'] )
                . "' LIMIT 1";
            $res=$this->ccdSQL->query( $query );
            $project=$this->ccdSQL->fetch_assoc( $res );
            $aFormValues['project_id']=$project['obj_key_project_id'];
        }
        if ( is_array( $_FILES[$aFormValues['filefield']] ) )
        {
            switch ( $aFormValues['root'] )
                {
                case "media":
                    $root_dir=$this->config->get( 'MEDIA_DIR' ) . '/';

                    $query=
                        "SELECT * FROM projects WHERE project_id='" . addslashes( $aFormValues['project_id'] )
                        . "' LIMIT 1";
                    $res=$this->ccdSQL->query( $query );

                    if ( $row=$this->ccdSQL->fetch_assoc( $res ) )
                    {
                        $aFormValues['path']='/' . $row['project_dir'] . $aFormValues['path'];
                    }
                    break;

                case "user":
                    $root_dir=$this->config->get( 'MEDIA_DIR' ) . '/';
                    $aFormValues['path'] .= '/';
		                 break;
                case "extensions":
                    $root_dir=$this->config->get( 'EXTENSION_DIR' ) . '/';
                    break;

                    default : $root_dir = $this->config->get('MEDIA_DIR').'/';
                    break;
                }


						$name = $this->tools->getRewriteFilename($_FILES[$aFormValues['filefield']]['name']);
            $dest=$root_dir . $aFormValues['path'] . "/" . $name;
            $res=@move_uploaded_file( $_FILES[$aFormValues['filefield']]['tmp_name'], $dest );

            if ( is_file( $dest ) )
                chmod( $dest, $this->config->get( 'FILE_CHMOD' ) );

            if ( $res )
            {
                $query=
                    "SELECT * FROM obj_index LEFT OUTER JOIN obj_names ON (obj_name_key_obj_id = obj_id AND obj_name_key_lang_id='"
                    . $_SESSION['CMS']['lang'][$aFormValues['project_id']]
                        . "')LEFT OUTER JOIN obj_versions_index ON obj_vers_key_obj_id = obj_id WHERE obj_id='"
                    . $aFormValues['obj_id'] . "'";
                $res=$this->ccdSQL->query( $query );
                $obj=$this->ccdSQL->fetch_assoc( $res );
                $this->smarty->assign( "obj", $obj );

                $img=array
                    (
                    "picture_filename" => $name,
                    "picture_dir" => $aFormValues['path'] . "/",
                    "picture_description" => ""
                    );

                $aFormValues['count']++;

                $id=rand( $aFormValues['count'], 100000 );

                $this->smarty->assign( "count", $id );
                $this->smarty->assign( "img", $img );
                $this->smarty->assign( "fieldname", $aFormValues['fieldname'] );
				
                $sHtml=$this->smarty->fetch( "forms/gallery_append.tpl" );
                $objResponse->contextAppend( "img_container.innerHTML", $sHtml );

                $objResponse->script( '$("#gallery_' . $aFormValues['fieldname'] . '_' . $id
                    . '_appear").slideDown("slow");' );
                $objResponse->call( 'Sortable.create',
                    'GallerySortable_' . $obj['obj_vers_id'] . '_' . $aFormValues['fieldname'], array
                    (
                    "tag" => "div",
                    "overlap" => 'horizontal',
                    "constraint" => false
                    ));
            }
            $objResponse->contextAssign( "container.style.display", 'block' );
        }

        return $objResponse;
    }

// -------------------------------------------------------------------------------------------------------------------
    function ContentEditorFilesUpload( $aFormValues )
    {

        $objResponse=new ccdsResponse();

        if ( "" == $aFormValues['project_id'] )
        {
            $query=
                "SELECT obj_key_project_id FROM obj_index WHERE obj_id='" . addslashes( $aFormValues['obj_id'] )
                . "' LIMIT 1";
            $res=$this->ccdSQL->query( $query );
            $project=$this->ccdSQL->fetch_assoc( $res );
            $aFormValues['project_id']=$project['obj_key_project_id'];
        }

        if ( is_array( $_FILES[$aFormValues['filefield']] ) )
        {
            switch ( $aFormValues['root'] )
                {
                case "media":
                    $root_dir=$this->config->get( 'MEDIA_DIR' ) . '/';

                    $query=
                        "SELECT * FROM projects WHERE project_id='" . addslashes( $aFormValues['project_id'] )
                        . "' LIMIT 1";
                    $res=$this->ccdSQL->query( $query );

                    if ( $row=$this->ccdSQL->fetch_assoc( $res ) )
                    {
                        $aFormValues['path']='/' . $row['project_dir'] . $aFormValues['path'];
                    }
                    break;

                case "extensions":
                    $root_dir=$this->config->get( 'EXTENSION_DIR' ) . '/';
                    break;

                case "user":
                    $root_dir=$this->config->get( 'MEDIA_DIR' ) . '/';
                    $aFormValues['path'] .= '/';
		                 break;
                    default : $root_dir = $this->config->get('MEDIA_DIR').'/';
                    break;
                }

						$file_name = $this->tools->getRewriteFilename($_FILES[$aFormValues['filefield']]['name']);

            $dest=$root_dir . $aFormValues['path'] . "/" . $file_name;
            $res=@move_uploaded_file( $_FILES[$aFormValues['filefield']]['tmp_name'], $dest );
            if ( is_file( $dest ) )
                chmod( $dest, $this->config->get( 'FILE_CHMOD' ) );
            if ( $res )
            {
                $query=
                    "SELECT * FROM obj_index LEFT OUTER JOIN obj_names ON (obj_name_key_obj_id = obj_id AND obj_name_key_lang_id='"
                    . $_SESSION['CMS']['lang'][$aFormValues['project_id']]
                        . "')LEFT OUTER JOIN obj_versions_index ON obj_vers_key_obj_id = obj_id WHERE obj_id='"
                    . $aFormValues['obj_id'] . "'";
                $res=$this->ccdSQL->query( $query );
                $obj=$this->ccdSQL->fetch_assoc( $res );
                $this->smarty->assign( "obj", $obj );

                $img=array
                    (
                    "file_name" => $_FILES[$aFormValues['filefield']]['name'],
                    "file_dir" => $aFormValues['path'] . "/",
                    "file_descr" => ""
                    );

                $aFormValues['count']++;

                $id=rand( $aFormValues['count'], 100000 );

                $this->smarty->assign( "count", $id );
                $this->smarty->assign( "item", $img );
                $this->smarty->assign( "fieldname", $aFormValues['fieldname'] );

                $sHtml=$this->smarty->fetch("forms/files_append.tpl" );
                $objResponse->contextAppend( "innerHTML", $sHtml );
								$objResponse->assign($aFormValues['fieldname'].'_'.$obj['obj_id'].'_container','style.display','block');
                $objResponse->call( 'Sortable.create',
                    'FilesSortable_' . $obj['obj_vers_id'] . '_' . $aFormValues['fieldname'], array
                    (
                    "tag" => "div",
                    "overlap" => 'horizontal',
                    "constraint" => false
                    ));
            }
        }

        return $objResponse;
    }
// -------------------------------------------------------------------------------------------------------------------

    function ContentEditorFileUpload( $aFormValues )
    {

        $objResponse=new ccdsResponse();

        if ( "" == $aFormValues['project_id'] )
        {
            $query=
                "SELECT obj_key_project_id FROM obj_index WHERE obj_id='" . addslashes( $aFormValues['obj_id'] )
                . "' LIMIT 1";
            $res=$this->ccdSQL->query( $query );
            $project=$this->ccdSQL->fetch_assoc( $res );
            $aFormValues['project_id']=$project['obj_key_project_id'];
        }

        if ( is_array( $_FILES['obj']['name'][$aFormValues['obj_id']][$aFormValues['fieldname']] ) )
        {
            switch ( $aFormValues['root'] )
                {
                case "media":
                    $root_dir=$this->config->get( 'MEDIA_DIR' ) . '/';

                    $query=
                        "SELECT * FROM projects WHERE project_id='" . addslashes( $aFormValues['project_id'] )
                        . "' LIMIT 1";
                    $res=$this->ccdSQL->query( $query );

                    if ( $row=$this->ccdSQL->fetch_assoc( $res ) )
                    {
                        $aFormValues['path']=$row['project_dir'] . $aFormValues['path'] . '/';
                    }
                    break;

                case "extensions":
                    $root_dir=$this->config->get( 'EXTENSION_DIR' ) . '/';
                    break;

                case "user":
                    $root_dir=$this->config->get( 'MEDIA_DIR' ) . '/';
                    $aFormValues['path'] .= '/';
		                 break;

                default : $root_dir = $this->config->get('MEDIA_DIR').'/';
                 break;
            }
            $file_name=$_FILES['obj']['name'][$aFormValues['obj_id']][$aFormValues['fieldname']]['up'];

						$file_name = $this->tools->getRewriteFilename($file_name);


            $file_tmp_name=$_FILES['obj']['tmp_name'][$aFormValues['obj_id']][$aFormValues['fieldname']]['up'];

						
            $dest=$root_dir . $aFormValues['path'] . "/" . $file_name;

            $res=@move_uploaded_file( $file_tmp_name, $dest );

            if ( is_file( $dest ) )
                chmod( $dest, $this->config->get( 'FILE_CHMOD' ) );

            if ( $res )
            {

					      $file_src = $dest;
					
					      $file_size = filesize($file_src);
								$file_size = $this->tools->formatBytes($file_size);
								$end = $this->tools->get_file_extension($file_src);
					
								$size_str = $file_size;

                $objResponse->contextAssign( 'label.innerHTML', $file_name );
                $objResponse->contextAssign( 'file.value', $file_name );
                $objResponse->contextAssign( 'dir.value', $aFormValues['path'] );

								$objResponse->contextAssign('size.innerHTML',(string) $size_str);
								$objResponse->contextAssign('type.innerHTML',(string) $end);
								$objResponse->contextAssign('anchor.href',(string) "/media/".$aFormValues['path'].$file_name);
								$objResponse->contextAssign('container.style.display','block');

                return $objResponse;
            }
        }

        return $objResponse;
    }

// -------------------------------------------------------------------------------------------------------------------

    function ContentEditorImageUpload( $aFormValues )
    {

        $objResponse=new ccdsResponse();

        if ( "" == $aFormValues['project_id'] )
        {
            $query=
                "SELECT obj_key_project_id FROM obj_index WHERE obj_id='" . addslashes( $aFormValues['obj_id'] )
                . "' LIMIT 1";
            $res=$this->ccdSQL->query( $query );
            $project=$this->ccdSQL->fetch_assoc( $res );
            $aFormValues['project_id']=$project['obj_key_project_id'];
        }

        if ( is_array( $_FILES['obj']['name'][$aFormValues['obj_id']][$aFormValues['fieldname']] ) )
        {
            switch ( $aFormValues['root'] )
                {
                case "media":
                    $root_dir=$this->config->get( 'MEDIA_DIR' ) . '/';

                    $query=
                        "SELECT * FROM projects WHERE project_id='" . addslashes( $aFormValues['project_id'] )
                        . "' LIMIT 1";
                    $res=$this->ccdSQL->query( $query );

                    if ( $row=$this->ccdSQL->fetch_assoc( $res ) )
                    {
                        $aFormValues['path']=$row['project_dir'] . $aFormValues['path'] . '/';
                    }
                    break;

                case "extensions":
                    $root_dir=$this->config->get( 'EXTENSION_DIR' ) . '/';
                    break;

		             case "user":
                    $root_dir=$this->config->get( 'MEDIA_DIR' ) . '/';
                    $aFormValues['path'] .= '/';
		             break;

                    default : $root_dir = $this->config->get('MEDIA_DIR').'/';
                    break;
                }

            $file_name=$_FILES['obj']['name'][$aFormValues['obj_id']][$aFormValues['fieldname']]['up'];
						$file_name = $this->tools->getRewriteFilename($file_name);

            $file_tmp_name=$_FILES['obj']['tmp_name'][$aFormValues['obj_id']][$aFormValues['fieldname']]['up'];

            $dest=$root_dir . $aFormValues['path'] . $file_name;
            $res=@move_uploaded_file( $file_tmp_name, $dest );

            if ( is_file( $dest ) )
                chmod( $dest, $this->config->get( 'FILE_CHMOD' ) );

            if ( $res )
            {

                $iCache=ccdsImageCache::getInstance();

                $img=$iCache->img_cache_return_array( array
                    (
                    "src" => "/media/" . $aFormValues['path'] . $file_name,
                    "id" => "img" . $aFormValues['fieldname'] . "_" . $aFormValues['obj_id'],
                    "width" => 125,
                    "height" => 125
                    ));



					      $img_src = $this->config->get('MEDIA_DIR').'/'.$aFormValues['path'].$file_name;
					      $size = $iCache->image_info($img_src);
					
					      $file_size = filesize($img_src);
								$file_size = $this->tools->formatBytes($file_size);
								$end = $this->tools->get_file_extension($img_src);
					
								$size_str = $size['width'].'x'.$size['height'].', '.$file_size;

								$objResponse->contextAssign('img.style.backgroundImage','url('.$img['src'].')');

                $objResponse->contextAssign( 'label.innerHTML', $file_name );
                $objResponse->contextAssign( 'file.value', $file_name );
                $objResponse->contextAssign( 'dir.value', $aFormValues['path'] );

								$objResponse->contextAssign('size.innerHTML',(string) $size_str);
								$objResponse->contextAssign('type.innerHTML',(string) $end);
								$objResponse->contextAssign('anchor.href',(string) "/media/".$aFormValues['path'].$file_name);
								$objResponse->contextAssign('container.style.display','block');

                return $objResponse;
            }
        }

        return $objResponse;
    }
// -------------------------------------------------------------------------------------------------------------------

    function ContentEditorParameter( $aFormValues )
    {

        $objResponse=new ccdsResponse();

        if ( "" == $aFormValues['project_id'] )
        {
            $query=
                "SELECT obj_key_project_id FROM obj_index WHERE obj_id='" . addslashes( $aFormValues['obj_id'] )
                . "' LIMIT 1";
            $res=$this->ccdSQL->query( $query );
            $project=$this->ccdSQL->fetch_assoc( $res );
            $aFormValues['project_id']=$project['obj_key_project_id'];
        }

        $_SESSION['CMS']['ContentEditor']="parameter";

        $obj=
            $this->tools->getObjectDetails( $aFormValues['obj_id'],
                $_SESSION['CMS']['lang'][$aFormValues['project_id']], false );
        $obj['permissions']=
            $this->tools->PermissionsObj2UserGet( $_SESSION['CMS']['user_id'], $obj['obj_id'],
                $_SESSION['CMS']['user_level'] );
        $this->smarty->assign( "obj", $obj );

        $classes=$this->tools->getObjectClasses( $aFormValues['obj_id'] );
        $this->smarty->assign( "classes", $classes );
        $query="SELECT * FROM obj_index WHERE obj_parent_obj_id='" . $aFormValues['obj_id'] . "'";
        $res=$this->ccdSQL->query( $query );
        $elements=array();

        while ( $row=$this->ccdSQL->fetch_assoc( $res ) )
        {
            $elements[]=$row;
        }
        $this->smarty->assign( "elements", $elements );

        if ( $aFormValues['obj_vers_id'] == "" )
        {

            $query=
                "SELECT * FROM obj_versions_index WHERE obj_vers_key_obj_id='" . $aFormValues['obj_id']
                . "' AND obj_vers_key_lang_id='" . $_SESSION['CMS']['lang'][$aFormValues['project_id']]
                . "' ORDER BY obj_vers_created desc LIMIT 1";
            $res=$this->ccdSQL->query( $query );

            if ( $row=$this->ccdSQL->fetch_assoc( $res ) )
            {
                $obj_vers_id=$row['obj_vers_id'];
            }
            else
            {
                $obj_vers_id=false;
            }
        }

        $object=new ccdsObject( $aFormValues['obj_id'] );
        $aFormFields=$object->getParameterArray( $_SESSION['CMS']['lang'][$aFormValues['project_id']] );

        $this->smarty->assign( "aFormFields", $aFormFields );

        $sHtml=$this->smarty->fetch( $this->tpl_dir . "content_parameter.tpl" );
        $objResponse->assign( "ContentParameter_" . $aFormValues['obj_id'], "innerHTML", $sHtml );

        if ( is_array( $aFormFields ) )
        {
            foreach ( $aFormFields as $FormField )
            {
                if ( $FormField['formtype'] == "html" )
                {

										$tab_id="TabManagerContent_" . $obj['obj_id'];
										$panel_id="Cparameter" . $obj['obj_id'];
										
		                $tiny_id="obj_" . $obj['obj_id'] . "_" . $FormField['fieldname'];
		                $objResponse->script( "
				            tinyMCE.execCommand('mceAddControl', false, '" . $tiny_id . "');
				            " );
		                //	            	xajax.ext.tabManager.showPanel('".$tab_id."','".$panel_id."');
		
		                $objResponse->clsTabManager->on( 'destroy', $tab_id, $panel_id, "function() {
			            	tinyMCE.execCommand('mceFocus', false, '" . $tiny_id . "');
			            	tinyMCE.execCommand('mceRemoveControl', false, '" . $tiny_id . "');
										}" );                	

                }
            }
        }
        return $objResponse;
    }

// -------------------------------------------------------------------------------------------------------------------

    function ContentEditorSave( $aFormValues )
    {

        $objResponse=new ccdsResponse();
        if ( is_array( $aFormValues['obj'] ) )
        {
            foreach ( $aFormValues['obj'] as $obj_id => $obj_values )
            {
            	
            	$this->tools->log($_SESSION['CMS']['user_id'],$obj_id,'Inhalt speichern');
            	
	            $query="SELECT obj_key_project_id FROM obj_index WHERE obj_id='" . addslashes( $obj_id ) . "' LIMIT 1";
	            $res=$this->ccdSQL->query( $query );
	            $project=$this->ccdSQL->fetch_assoc( $res );
	            $aFormValues['project_id']=$project['obj_key_project_id'];
	
	            $objResponse->clsSWFupload->destroyForm( "form_content_id_" . $obj_id );
	            $obj=
	                $this->tools->getObjectDetails( $obj_id, $_SESSION['CMS']['lang'][$aFormValues['project_id']],
	                    false );
	            $obj['permissions']=
	                $this->tools->PermissionsObj2UserGet( $_SESSION['CMS']['user_id'], $obj['obj_id'],
	                    $_SESSION['CMS']['user_level'] );
	
	            $this->ccdSQL->query( "UPDATE obj_index SET obj_last_edit='" . time() . "', obj_last_edit_key_user_id='"
	                . $_SESSION['CMS']['user_id'] . "' WHERE obj_id='" . $obj_id . "'" );
	
							$foo = array('object' => &$object, 'aFormValues' => &$aFormValues, 'lang_id' => $_SESSION['CMS']['lang'][$aFormValues['project_id']] );
							$this->modules->call('ccdsContentEditorBeforeSave',$foo);
	
	            $object=new ccdsObject( $obj_id );
	            $obj_vers_id=$object->save( $obj_values, $_SESSION['CMS']['lang'][$aFormValues['project_id']] );
	
	
							$foo = array('object' => &$object, 'aFormValues' => &$aFormValues, 'lang_id' => $_SESSION['CMS']['lang'][$aFormValues['project_id']], 'obj_vers_id' => $obj_vers_id, 'objResponse' => &$objResponse );
							$this->modules->call('ccdsContentEditorSave',$foo);
	
							$objResponse->assign('obj_vers_id_'.$obj_id,'value',$obj_vers_id);
	
	            $query=
	                "UPDATE obj_versions_index SET obj_vers_visible=0 WHERE obj_vers_key_obj_id=" . $obj['obj_id']
	                . " AND obj_vers_key_lang_id=" . $_SESSION['CMS']['lang'][$aFormValues['project_id']];
	            $res=$this->ccdSQL->query( $query );
	
	            $query="UPDATE obj_versions_index SET obj_vers_visible=1 WHERE obj_vers_id=" . $obj_vers_id;
	            $res=$this->ccdSQL->query( $query );
	
	            $obj=$object->getObject( $_SESSION['CMS']['lang'][$aFormValues['project_id']], true );
	            $obj['permissions']=
	                $this->tools->PermissionsObj2UserGet( $_SESSION['CMS']['user_id'], $obj['obj_id'],
	                    $_SESSION['CMS']['user_level'] );
	            $this->smarty->assign( "obj", $obj );
	
	            if ( $object->version_control() )
	            {
	                $obj_versions_index=array();
	                $query=
	                    "SELECT * FROM obj_versions_index LEFT OUTER JOIN user ON obj_vers_creator = user_id
		           				WHERE obj_vers_key_obj_id='" . $obj_id . "' AND obj_vers_key_lang_id = '"
	                    . $_SESSION['CMS']['lang'][$aFormValues['project_id']]
	                    . "' ORDER BY obj_vers_created desc LIMIT 10";
	                $res=$this->ccdSQL->query( $query );
	
	                while ( $row=$this->ccdSQL->fetch_assoc( $res ) )
	                {
	                    $obj_versions[]=$row;
	                }
	
	                $this->smarty->assign( "obj_versions", $obj_versions );
	                $this->smarty->assign( "obj_vers_id", $obj_vers_id );
	                $sHtml=$this->smarty->fetch( $this->tpl_dir . "content_edit_versions.tpl" );
	                $objResponse->assign( "obj_" . $obj_id . "_versions_div", "innerHTML", $sHtml );
	            }
	        		$this->tools->notify($objResponse,"Objekt \"".$obj['obj_name_name']."\" gespeichert.");
            }
        }
        return $objResponse;
    }
// -------------------------------------------------------------------------------------------------------------------

    function ContentEditorVersionSave( $aFormValues )
    {
	    if ( "" == $aFormValues['project_id'] )
	    {
	
	        $query=
	            "SELECT obj_key_project_id FROM obj_index WHERE obj_id='" . addslashes( $aFormValues['obj_id'] )
	            . "' LIMIT 1";
	        $res=$this->ccdSQL->query( $query );
	        $project=$this->ccdSQL->fetch_assoc( $res );
	        $aFormValues['project_id']=$project['obj_key_project_id'];
	    }


      $this->tools->log($_SESSION['CMS']['user_id'],$aFormValues['obj_id'],'Version speichern');

	
	    $objResponse=new ccdsResponse();
	    $this->ccdSQL->query( "UPDATE obj_versions_index SET obj_vers_visible='0' WHERE obj_vers_key_obj_id='"
	        . $aFormValues['obj_id'] . "' AND obj_vers_key_lang_id='"
	        . $_SESSION['CMS']['lang'][$aFormValues['project_id']] . "'" );
	    $this->ccdSQL->query( "UPDATE obj_versions_index SET obj_vers_visible='1' WHERE obj_vers_id='"
	        . $aFormValues['obj_vers_id'] . "'" );
				$query = "DELETE FROM obj_versions WHERE obj_vers_key_obj_id='".addslashes($aFormValues['obj_id'])."'";
			$this->ccdSQL->query($query);
	    $query="INSERT INTO obj_versions
					(
						obj_vers_id,
						obj_vers_key_obj_id,
						obj_vers_key_lang_id
					)
					VALUES (
						'". addslashes($aFormValues['obj_vers_id'])  ."',
						'". addslashes($aFormValues['obj_id']) ."',
						'". $_SESSION['CMS']['lang'][$aFormValues['project_id']] . "'
					)
				 ";
			$this->ccdSQL->query($query);
	   	$this->tools->notify($objResponse,"Objekt-Version wurde gespeichert.");
	
	    return $objResponse;
    }
// -------------------------------------------------------------------------------------------------------------------

    function ContentEditorParameterSave( $aFormValues )
    {
	    $objResponse=new ccdsResponse();
	
	    if ( is_array( $aFormValues['obj'] ) )
	    {
	        foreach ( $aFormValues['obj'] as $obj_id => $obj_values )
	        {

            	$this->tools->log($_SESSION['CMS']['user_id'],$obj_id,'ParameterInhalt speichern');

	            if ( "" == $obj_values['project_id'] )
	            {
	                $query=
	                    "SELECT obj_key_project_id FROM obj_index WHERE obj_id='" . addslashes( $obj_id ) . "' LIMIT 1";
	                $res=$this->ccdSQL->query( $query );
	                $project=$this->ccdSQL->fetch_assoc( $res );
	                $obj_values['project_id']=$project['obj_key_project_id'];
	            }
	
	            $this->ccdSQL->query( "UPDATE obj_index SET obj_last_edit='" . time() . "', obj_last_edit_key_user_id='"
	                . $_SESSION['CMS']['user_id'] . "' WHERE obj_id='" . $obj_id . "'" );
	            $object=new ccdsObject( $obj_id );
	            $foo=$object->saveParameter( $obj_values, $_SESSION['CMS']['lang'][$obj_values['project_id']] );
							
							$name = $object->getName($_SESSION['CMS']['lang'][$obj_values['project_id']]);
							$objResponse->script("$.jGrowl('Objekt-Voreinstellungen &quot;".$name['obj_name_name']."&quot; gespeichert.',{ header: '".date("H:i")." Uhr' });");
	
	        }
	    }
	    return $objResponse;
    }
// -------------------------------------------------------------------------------------------------------------------

    function ContentEditorMarkObjectDeleted( $aFormValues )
    {

      $objResponse=new ccdsResponse();
      $obj_id=(int)$aFormValues['obj_id'];
      $query="SELECT * FROM obj_index WHERE obj_id='" . $obj_id . "'";
      $res=$this->ccdSQL->query( $query );
      $obj=$this->ccdSQL->fetch_assoc( $res );
      $this->tools->MarkObjectDeleted( $obj_id, true );

      $objResponse->loadClassCommand( "ccdsDefaultPlugin", "updateNaviEntry", array
          (
          "project_id" => $obj['obj_key_project_id'],
          "obj_parent_obj_id" => $obj['obj_parent_obj_id']
          ), true );

      $objResponse->loadClassCommand(
          "ccdsTrash", "TrashReload", ( array("project_id" => $obj['obj_key_project_id'])) );

      return $objResponse;
    }

// -------------------------------------------------------------------------------------------------------------------
    function ContentEditorPermissions( $aFormValues )
    {
      if ( "" == $aFormValues['project_id'] )
      {

          $query=
              "SELECT obj_key_project_id FROM obj_index WHERE obj_id='" . addslashes( $aFormValues['obj_id'] )
              . "' LIMIT 1";
          $res=$this->ccdSQL->query( $query );
          $project=$this->ccdSQL->fetch_assoc( $res );
          $aFormValues['project_id']=$project['obj_key_project_id'];
      }

      $objResponse=new ccdsResponse();

      $obj=
          $this->tools->getObjectDetails( $aFormValues['obj_id'],
              $_SESSION['CMS']['lang'][$aFormValues['project_id']], false );
      ;

      $this->smarty->assign( "obj", $obj );

      $classes=$this->tools->getObjectClasses( $aFormValues['obj_id'] );
      $this->smarty->assign( "classes", $classes );
      $groups=Array();
      $query="SELECT * FROM groups order by group_name";
      $res=$this->ccdSQL->query( $query );

      while ( $row=$this->ccdSQL->fetch_assoc( $res ) )
      {
          $tmprow=$row;
          $tmprow['permissions']=$this->tools->getObjGroupPermissions( $aFormValues['obj_id'], $tmprow['group_id'] );
          $tmprow['user']=array();
          $query=
              "SELECT user.* FROM user LEFT JOIN mn_user_groups ON user_groups_key_user_id=user_id WHERE user_groups_key_group_id='"
              . $row['group_id'] . "'";
          $res_user=$this->ccdSQL->query( $query );

          while ( $row_user=$this->ccdSQL->fetch_assoc( $res_user ) )
          {
              $row_user['permissions']=
                  $this->tools->getObjUserPermissions( $aFormValues['obj_id'], $tmprow['group_id'],
                      $row_user['user_id'] );
              $tmprow['user'][]=$row_user;
          }
          $groups[]=$tmprow;
      }
      $this->smarty->assign( "groups", $groups );
      $sHtml=$this->smarty->fetch( $this->tpl_dir . "content_permissions.tpl" );
      $objResponse->assign( "ContentPermissions_" . $obj['obj_id'], "innerHTML", $sHtml );
      return $objResponse;
    }


// -------------------------------------------------------------------------------------------------------------------
    function ContentEditorPermissionsSave( $aFormValues )
    {

      $objResponse=new ccdsResponse();

      $this->ccdSQL->query( "DELETE FROM permissions_obj_index_2_group WHERE obj_index_2_group_key_obj_id='"
          . $values['obj_id'] . "'" );
      $this->ccdSQL->query( "DELETE FROM permissions_obj_index_2_user WHERE obj_index_2_user_key_obj_id='"
          . $values['obj_id'] . "'" );

      $regex="group_" . $group_id . "_user_([0-9]?)_right";
      $sHtml.=date( "d.m.Y H:i" ) . "<br />";

      $query="SELECT * FROM groups order by group_name";
      $res=$this->ccdSQL->query( $query );

      while ( $row=$this->ccdSQL->fetch_assoc( $res ) )
      {
          $query=
              "SELECT user.* FROM user LEFT JOIN mn_user_groups ON user_groups_key_user_id=user_id WHERE user_groups_key_group_id='"
              . $row['group_id'] . "'";
          $res_user=$this->ccdSQL->query( $query );

          $this->ccdSQL->query( "INSERT INTO permissions_obj_index_2_group
                (
                obj_index_2_group_key_obj_id,
                obj_index_2_group_key_group_id,
                obj_index_2_group_read,
                obj_index_2_group_modify,
                obj_index_2_group_delete,
                obj_index_2_group_new
                )
                VALUES (
                '" . $values['obj_id'] . "',
                '" . $row['group_id'] . "',
                '" . $values['group_' . $row['group_id'] . '_read'] . "',
                '" . $values['group_' . $row['group_id'] . '_modfiy'] . "',
                '" . $values['group_' . $row['group_id'] . '_delete'] . "',
                '" . $values['group_' . $row['group_id'] . '_new'] . "'
                )
                " );
          $sHtml.=$this->ccdSQL->error() . "<br />";

          while ( $row_user=$this->ccdSQL->fetch_assoc( $res_user ) )
          {
              $this->ccdSQL->query( "INSERT INTO permissions_obj_index_2_user
                    (
                    obj_index_2_user_key_obj_id,
                    obj_index_2_user_key_group_id,
                    obj_index_2_user_key_user_id,
                    obj_index_2_user_read,
                    obj_index_2_user_modify,
                    obj_index_2_user_delete,
                    obj_index_2_user_new
                    )
                    VALUES (
                    '" . $values['obj_id'] . "',
                    '" . $row['group_id'] . "',
                    '" . $row_user['user_id'] . "',
                    '" . $values['group_' . $row['group_id'] . '_user_' . $row_user['user_id'] . '_read'] . "',
                    '" . $values['group_' . $row['group_id'] . '_user_' . $row_user['user_id'] . '_modify'] . "',
                    '" . $values['group_' . $row['group_id'] . '_user_' . $row_user['user_id'] . '_delete'] . "',
                    '" . $values['group_' . $row['group_id'] . '_user_' . $row_user['user_id'] . '_new'] . "'
                    )
                    " );
          }
      }
      return $objResponse;
    }

// -------------------------------------------------------------------------------------------------------------------
    function ContentEditorPermissionsClassesOpen( $aFormValues )
    {
			if ( "" == $aFormValues['project_id'] )
			{
			
			    $query=
			        "SELECT obj_key_project_id FROM obj_index WHERE obj_id='" . addslashes( $aFormValues['obj_id'] )
			        . "' LIMIT 1";
			    $res=$this->ccdSQL->query( $query );
			    $project=$this->ccdSQL->fetch_assoc( $res );
			    $aFormValues['project_id']=$project['obj_key_project_id'];
			}
			
			$objResponse=new ccdsResponse();
			
			$obj=
			    $this->tools->getObjectDetails( $aFormValues['obj_id'],
			        $_SESSION['CMS']['lang'][$aFormValues['project_id']], false );
			$this->smarty->assign( "obj", $obj );
			$classes=array();
			$query="SELECT
						*
						FROM
						classes
						LEFT JOIN
						permissions_class_2_class ON class_2_class_child_id = class_id
						
						LEFT OUTER JOIN permissions_obj_index_2_class ON
						(
							obj_index_class_key_class_id = class_2_class_child_id
							AND
							obj_index_class_key_obj_id = '" . $obj['obj_id'] . "'
						)
						
						WHERE
						class_2_class_node_id='" . $obj['obj_key_class_id'] . "'
						ORDER BY
						class_name
			   ";
			$res=$this->ccdSQL->query( $query );
			
			while ( $row=$this->ccdSQL->fetch_assoc( $res ) )
			{
			    $classes[]=$row;
			}
			
			$this->smarty->assign( "classes", $classes );
			$sHtml=$this->smarty->fetch( $this->tpl_dir . "content_classes.tpl" );
			$objResponse->assign( "ContentPermissionClasses_" . $obj['obj_id'], "innerHTML", $sHtml );
			return $objResponse;
    }

// -------------------------------------------------------------------------------------------------------------------


// -------------------------------------------------------------------------------------------------------------------
    function ContentEditorPermissionsClassesSave( $aFormValues )
    {

	    $objResponse=new ccdsResponse();
	
	    $this->ccdSQL->query( "DELETE FROM permissions_obj_index_2_class WHERE obj_index_class_key_obj_id='"
	        . addslashes( $aFormValues['obj_id'] ) . "'" );
	
	    if ( $aFormValues['apply_linked'] == 1 )
	    {
	        $query="SELECT obj_id FROM obj_index WHERE obj_link_obj_id='" . addslashes( $aFormValues['obj_id'] ) . "'";
	        $res=$this->ccdSQL->query( $query );
	
	        while ( $row=$this->ccdSQL->fetch_assoc( $res ) )
	        {
	            $this->ccdSQL->query( "DELETE FROM permissions_obj_index_2_class WHERE obj_index_class_key_obj_id='"
	                . addslashes( $row['obj_id'] ) . "'" );
	        }
	    }
	
	    if ( is_array( $aFormValues['class'] ) )
	        foreach ( $aFormValues['class'] as $class )
	        {
	            $this->ccdSQL->query( "INSERT INTO permissions_obj_index_2_class VALUES('"
	                . addslashes( $aFormValues['obj_id'] ) . "','" . addslashes( $class ) . "')" );
	
	            if ( $aFormValues['apply_linked'] == 1 )
	            {
	                $query=
	                    "SELECT obj_id FROM obj_index WHERE obj_link_obj_id='" . addslashes( $aFormValues['obj_id'] )
	                    . "'";
	                $res=$this->ccdSQL->query( $query );
	
	                while ( $row=$this->ccdSQL->fetch_assoc( $res ) )
	                {
	                    $this->ccdSQL->query( "INSERT INTO permissions_obj_index_2_class VALUES('" . $row['obj_id']
	                        . "','" . addslashes( $class ) . "')" );
	                }
	            }
	        }
	    $objResponse->loadcommands( $this->ContentEditorOverview( $aFormValues ) );
	    return $objResponse;
    }


// -------------------------------------------------------------------------------------------------------------------

    function ContentEditorOverviewUpdate( $aFormValues )
    {
	    if ( "" == $aFormValues['project_id'] )
	    {
	
	        $query=
	            "SELECT obj_key_project_id FROM obj_index WHERE obj_id='" . addslashes( $aFormValues['obj_id'] )
	            . "' LIMIT 1";
	        $res=$this->ccdSQL->query( $query );
	        $project=$this->ccdSQL->fetch_assoc( $res );
	        $aFormValues['project_id']=$project['obj_key_project_id'];
	    }
	
	    $objResponse=new ccdsResponse();
	
	

	
	    $project_languages=array();
	    $query=
	        "
	       SELECT * FROM mn_project_language LEFT JOIN languages ON project_language_key_language_id=lang_id WHERE project_language_key_project_id='"
	        . $aFormValues['project_id'] . "'";
	    $res=$this->ccdSQL->query( $query );
	
	    while ( $row=$this->ccdSQL->fetch_assoc( $res ) )
	    {
	        if ( $aFormValues['obj_is_public'][$row['lang_id']] == "on" )
	        {
	            $aFormValues['obj_is_public'][$row['lang_id']]=1;

				     $this->tools->log($_SESSION['CMS']['user_id'],$aFormValues['obj_id'],'Freigabe an');

	        }
	        else
	        {
	            $aFormValues['obj_is_public'][$row['lang_id']]=0;

				     $this->tools->log($_SESSION['CMS']['user_id'],$aFormValues['obj_id'],'Freigabe aus');

	        }
	
	        if ( $aFormValues['visible_childs'][$row['lang_id']] == "on" )
	        {
	            $aFormValues['visible_childs'][$row['lang_id']]=true;
	        }
	        else
	        {
	            $aFormValues['visible_childs'][$row['lang_id']]=false;
	        }
	        $this->update_visibility( $aFormValues['obj_id'], $row['lang_id'],
	            $aFormValues['obj_is_public'][$row['lang_id']], $aFormValues['visible_childs'][$row['lang_id']] );
	
	        if ( $row['lang_id'] == $_SESSION['CMS']['lang'][$aFormValues['project_id']] )
	        {
	            if ( 1 == $aFormValues['obj_is_public'][$row['lang_id']] )
	            {
	                $objResponse->script( '
					$("#CMS_project_tree_' . $aFormValues['project_id'] . '").xTree("enable","navi_obj_' . $aFormValues['obj_id'] . '", ' . json_encode( $aFormValues['visible_childs'][$row['lang_id']] ) . ');		
				' );
	            }
	            else
	            {
	                $objResponse->script( '
					$("#CMS_project_tree_' . $aFormValues['project_id'] . '").xTree("disable","navi_obj_' . $aFormValues['obj_id'] . '", ' . json_encode( $aFormValues['visible_childs'][$row['lang_id']] ) . ');		
				' );
	            }
	        }
	
	        if ( $aFormValues['apply_linked'] == 1 )
	        {
	            $query="SELECT obj_id FROM obj_index WHERE obj_link_obj_id='" . $aFormValues['obj_id'] . "'";
	            $res2=$this->ccdSQL->query( $query );
	
	            while ( $row2=$this->ccdSQL->fetch_assoc( $res2 ) )
	            {
	                $this->update_visibility( $row2['obj_id'], $row['lang_id'],
	                    $aFormValues['obj_is_public'][$row['lang_id']],
	                    $aFormValues['visible_childs'][$row['lang_id']] );
	            }
	        }
	    }
	   	$this->tools->notify($objResponse,"Die Einstellungen wurden gespeichert.");
	
	    return $objResponse;
    }
// -------------------------------------------------------------------------------------------------------------------

    function ContentEditorExpire( $aFormValues )
    {

      $objResponse=new ccdsResponse();

      if ( $aFormValues['obj_expire'] != 1 )
      {
          $aFormValues['obj_expire']=0;
          $aFormValues['obj_expire_start']=0;
          $aFormValues['obj_expire_end']=0;
      }
      else
      {
          $aFormValues['obj_expire_start']=
              mktime( $aFormValues['obj_expire_start_hour'], $aFormValues['obj_expire_start_minute'], 0,
                  $aFormValues['obj_expire_start_month'], $aFormValues['obj_expire_start_day'],
                  $aFormValues['obj_expire_start_year'] );
          $aFormValues['obj_expire_end']=
              mktime( $aFormValues['obj_expire_end_hour'], $aFormValues['obj_expire_end_minute'], 0,
                  $aFormValues['obj_expire_end_month'], $aFormValues['obj_expire_end_day'],
                  $aFormValues['obj_expire_end_year'] );
      }

      if ( $aFormValues['obj_expire_del'] != 1 )
          $aFormValues['obj_expire_del']=0;

      $query="
         UPDATE obj_index SET
         obj_expire = '" . $aFormValues['obj_expire'] . "',
         obj_expire_start = '" . $aFormValues['obj_expire_start'] . "',
         obj_expire_end = '" . $aFormValues['obj_expire_end'] . "',
         obj_expire_del = '" . $aFormValues['obj_expire_del'] . "'
         WHERE
         obj_id = '" . $aFormValues['obj_id'] . "'

         ";
      $this->ccdSQL->query( $query );

      if ( $aFormValues['apply_linked'] == 1 )
      {
          $query="SELECT obj_id FROM obj_index WHERE obj_link_obj_id='" . $aFormValues['obj_id'] . "'";
          $res=$this->ccdSQL->query( $query );

          while ( $row=$this->ccdSQL->fetch_assoc( $res ) )
          {
              $query="
                 UPDATE obj_index SET
                 obj_expire = '" . $aFormValues['obj_expire'] . "',
                 obj_expire_start = '" . $aFormValues['obj_expire_start'] . "',
                 obj_expire_end = '" . $aFormValues['obj_expire_end'] . "',
                 obj_expire_del = '" . $aFormValues['obj_expire_del'] . "'
                 WHERE
                 obj_id = '" . $row['obj_id'] . "'
                 ";
              $this->ccdSQL->query( $query );
          }
      }
      $objResponse->script(
          'new Effect.Highlight("DivContentEditorExpire",{startcolor:"#ffffff",endcolor:"#D6EAFB",restorecolor:"#ffffff"});' );

      return $objResponse;
    }
// -------------------------------------------------------------------------------------------------------------------

    function ContentEditorOverviewAction( $aFormValues )
    {

        $objResponse=new ccdsResponse();

        $query="SELECT * FROM obj_index WHERE obj_id='" . $aFormValues['obj_id'] . "'";
        $res=$this->ccdSQL->query( $query );
        $obj=$this->ccdSQL->fetch_assoc( $res );

        $obj_names=array();
        $query="
          SELECT
          *
          FROM
          obj_names

          WHERE
          obj_name_key_obj_id='" . $aFormValues['obj_id'] . "'
          ";
        $res=$this->ccdSQL->query( $query );

        while ( $obj_name=$this->ccdSQL->fetch_assoc( $res ) )
        {
            $obj_names[$obj_name['obj_name_key_lang_id']]=$obj_name['obj_name_name'];
        }

        $aFormValues['obj_action_recursive']=isset( $aFormValues['obj_action_recursive'] ) ? true : false;

        switch ( $aFormValues['obj_action'] )
            {
            case "copy":
                $_SESSION['CMS']['ACTION']['obj_id']=$aFormValues['obj_id'];
                $_SESSION['CMS']['ACTION']['recursive']=$aFormValues['obj_action_recursive'];
                $_SESSION['CMS']['ACTION']['do']="copy";
                $_SESSION['CMS']['ACTION']['name']=$obj_names;
                $_SESSION['CMS']['ACTION']['obj']=$obj;
                break;

            case "cut":
                $_SESSION['CMS']['ACTION']['obj_id']=$aFormValues['obj_id'];
                $_SESSION['CMS']['ACTION']['recursive']=$aFormValues['obj_action_recursive'];
                $_SESSION['CMS']['ACTION']['do']="cut";
                $_SESSION['CMS']['ACTION']['name']=$obj_names;
                $_SESSION['CMS']['ACTION']['obj']=$obj;
                break;

            //hinzugefügt von Steffen: Objekt leeren
            case "clear":
                $query="SELECT obj_id FROM obj_index WHERE obj_parent_obj_id = '" . $aFormValues['obj_id'] . "'  AND obj_deleted=0";
                $res=$this->ccdSQL->query( $query );

                while ( $row=$this->ccdSQL->fetch_assoc( $res ) )
                {
                    $query_links=
                        "SELECT COUNT(obj_id) as anzahl FROM obj_index WHERE obj_link_obj_id='" . $row['obj_id'] . "' AND obj_deleted=0";
                    $res_links=$this->ccdSQL->query( $query_links );
                    $row_links=$this->ccdSQL->fetch_assoc( $res_links );

                    $this->tools->MarkObjectDeleted( $row['obj_id'], true );

                    $objResponse->clsTabManager->close( "topTab", "tab_" . $row['obj_id'] );
 
                }

                $objResponse->loadClassCommand( "ccdsDefaultPlugin", "updateNaviEntry", array
                    (
                    "project_id" => $obj['obj_key_project_id'],
                    "obj_parent_obj_id" => $obj['obj_parent_obj_id']
                    ), true );

                if ( $aFormValues['frontend'] )
                    $objResponse->script( "xajax.$('CMS_FRONTEND_IFRAME').contentWindow.location.reload();" );

                $objResponse->loadClassCommand(
                    "ccdsTrash", "TrashReload", ( array("project_id" => $obj['obj_key_project_id'])) );
                break;

            case "del":




                $query=
                    "SELECT COUNT(obj_id) as anzahl FROM obj_index WHERE obj_link_obj_id='" . $aFormValues['obj_id']
                    . "'";
                $res=$this->ccdSQL->query( $query );
                $row=$this->ccdSQL->fetch_assoc( $res );

                if ( $row['anzahl'] > 0 AND $aFormValues['confirmed'] != 1 )
                {
                    $objResponse->script( "
                    
                    $('<div />').xWindow('confirm',{title:'Loeschen',content:'Das gewählte Objekt ist " . $row['anzahl']
                        . " mal verlinkt.<br/><br/>Wollen Sie dieses Objekt wirklich löschen? Es gehen alle Links verloren!'},
                    			function(){xajax.request({ccdsfun:'ContentEditorOverviewAction',ccdsclass:'ccdsContentEditor'},
                    			
                    			{parameters:[{obj_id:". $aFormValues['obj_id'] . ",'obj_action':'del','confirmed':1}]}
                    			);
                    			
                    			return true;});
                    			
                    			" );
                }
                else
                {

						      $this->tools->log($_SESSION['CMS']['user_id'],$aFormValues['obj_id'],'Objekt löschen');

                    $this->tools->MarkObjectDeleted( $aFormValues['obj_id'], true );

                    $objResponse->loadClassCommand( "ccdsDefaultPlugin", "updateNaviEntry", array
                        (
                        "project_id" => $obj['obj_key_project_id'],
                        "obj_parent_obj_id" => $obj['obj_parent_obj_id']
                        ), true );

                    $objResponse->clsTabManager->close( "topTab", "tab_" . $aFormValues['obj_id'] );

                    if ( $row['anzahl'] > 0 )
                    {
                        $query=
                            "SELECT obj_id,obj_parent_obj_id FROM obj_index WHERE obj_link_obj_id='"
                            . $aFormValues['obj_id'] . "'";
                        $res=$this->ccdSQL->query( $query );

                        while ( $row=$this->ccdSQL->fetch_assoc( $res ) )
                        {
                            $objResponse->clsTabManager->close( "topTab", "tab_" . $row['obj_id'] );
                        }
                    }

                    if ( $aFormValues['frontend'] )
                        $objResponse->script( "xajax.$('CMS_FRONTEND_IFRAME').contentWindow.location.reload();" );

                    $objResponse->loadClassCommand(
                        "ccdsTrash", "TrashReload", ( array("project_id" => $obj['obj_key_project_id'])) );
                }
                break;

            case "clone":

                $new_obj_id=
                    $this->tools->copy_object( $aFormValues['obj_id'], $obj['obj_parent_obj_id'],
                        $_SESSION['CMS']['user_id'], $aFormValues['obj_action_recursive'],
                        $_SESSION['CMS']['ACTION']['recursive'] );
                $objResponse->loadClassCommand( "ccdsDefaultPlugin", "updateNaviEntry", array
                    (
                    "project_id" => $obj['obj_key_project_id'],
                    "obj_parent_obj_id" => $obj['obj_parent_obj_id']
                    ), true );

                break;

            case "paste":
                if ( $_SESSION['CMS']['ACTION']['do'] == "cut" )
                {

                    $query=
                        "SELECT MAX(obj_position) as pos FROM obj_index WHERE obj_parent_obj_id='"
                        . $aFormValues['obj_id'] . "'";
                    $res=$this->ccdSQL->query( $query );
                    $pos=$this->ccdSQL->fetch_assoc( $res );

                    $this->ccdSQL->query( "UPDATE obj_index SET obj_parent_obj_id='"
                        . addslashes( $aFormValues['obj_id'] ) . "',obj_position='" . ( $pos['pos'] + 1 )
                            . "' WHERE obj_id='" . $_SESSION['CMS']['ACTION']['obj_id'] . "'" );

                    $query=
                        "SELECT obj_parent_obj_id,obj_key_project_id FROM obj_index WHERE obj_id='"
                        . addslashes( $_SESSION['CMS']['ACTION']['obj_id'] ) . "'";
                    $res=$this->ccdSQL->query( $query );
                    $parent=$this->ccdSQL->fetch_assoc( $res );

                    $objResponse->loadClassCommand( "ccdsDefaultPlugin", "updateNaviEntry", array
                        (
                        "project_id" => $parent['obj_key_project_id'],
                        "obj_parent_obj_id" => $parent['obj_parent_obj_id']
                        ), true );
                }

                if ( $_SESSION['CMS']['ACTION']['do'] == "copy" )
                {
                    $new_obj_id=$this->tools->copy_object( $_SESSION['CMS']['ACTION']['obj']['obj_id'],
                        $aFormValues['obj_id'], $_SESSION['CMS']['user_id'], $_SESSION['CMS']['ACTION']['recursive'] );
                }

                $objResponse->loadClassCommand( "ccdsDefaultPlugin", "updateNaviEntry", array
                    (
                    "project_id" => $obj['obj_key_project_id'],
                    "obj_parent_obj_id" => $obj['obj_id']
                    ), true );

                $_SESSION['CMS']['ACTION']="";
                break;

                default:
                $_SESSION['CMS']['ACTION']="";
                break;
            }

        return $objResponse;
    }
// -------------------------------------------------------------------------------------------------------------------

    function ContentEditorDynamicTemplateNew( $aFormValues )
    {
      if ( "" == $aFormValues['project_id'] )
      {

          $query=
              "SELECT obj_key_project_id FROM obj_index WHERE obj_id='" . addslashes( $aFormValues['obj_id'] )
              . "' LIMIT 1";
          $res=$this->ccdSQL->query( $query );
          $project=$this->ccdSQL->fetch_assoc( $res );
          $aFormValues['project_id']=$project['obj_key_project_id'];
      }

      $objResponse=new ccdsResponse();

      $object=new ccdsObject( $aFormValues['obj_id'] );
      $obj=$object->getObject( $_SESSION['CMS']['lang'][$aFormValues['project_id']] );

      $ObjectConfig=ccdsObjectConfig::getInstance();
      $classConfig=$ObjectConfig->getClass( $obj['obj_key_class_id'] );

      $fieldConfig=&$classConfig['data']['columns'][$aFormValues['fieldname']];

      if ( !is_array( $fieldConfig['templates'] ) )
          $fieldConfig['templates']=array();

      return $objResponse;
    }

// -------------------------------------------------------------------------------------------------------------------

    function ContentEditorDynamicFieldAdd( $aFormValues )
    {
      if ( "" == $aFormValues['project_id'] )
      {

          $query=
              "SELECT obj_key_project_id FROM obj_index WHERE obj_id='" . addslashes( $aFormValues['obj_id'] )
              . "' LIMIT 1";
          $res=$this->ccdSQL->query( $query );
          $project=$this->ccdSQL->fetch_assoc( $res );
          $aFormValues['project_id']=$project['obj_key_project_id'];
      }

      $objResponse=new ccdsResponse();

      if ( "" == $aFormValues['field']['fieldlabel'] ) {
      //$objResponse->alert("Achtung, es wurde keine Bezeichnung angegeben");
      //return $objResponse;
      }

      $obj=
          $this->tools->getObjectDetails( $aFormValues['obj_id'],
              $_SESSION['CMS']['lang'][$aFormValues['project_id']] );
      $this->smarty->assign( "obj", $obj );

      $content=array
          (
          "label" => $aFormValues['field']['fieldlabel'],
          "fieldname" => $aFormValues['field']['fieldlabel'],
          "suffix" => "[value]",
          "obj_id" => $aFormValues['obj_id'],
          "obj_vers_id" => $aFormValues['obj_vers_id']
          );

      $this->smarty->assign( "content", $content );
      $sHtml=
          $this->smarty->fetch( $this->tpl_dir . "forms/" . $aFormValues['field']['fieldtype']
              . ".tpl" );

      $sHtml.="<input type=\"hidden\" name=\"" . $aFormValues['fieldname'] . "[" . $aFormValues['field']['fieldlabel']
          . "][formtype]" . "\" value=\"" . $aFormValues['field']['fieldtype'] . "\"/>";
      $sHtml.="<input type=\"hidden\" name=\"" . $aFormValues['fieldname'] . "[" . $aFormValues['field']['fieldlabel']
          . "][label]" . "\" value=\"" . $aFormValues['field']['fieldlabel'] . "\"/>";

      $objResponse->append( "dyn_area_" . $aFormValues['fieldname'] . "_" . $aFormValues['obj_id'], "innerHTML",
          $sHtml );

      return $objResponse;
    }


// -------------------------------------------------------------------------------------------------------------------

    function update_visibility( $obj_id, $lang_id, $value, $recursive=false )
    {
      $this->ccdSQL->query( "DELETE FROM obj_index_public WHERE objp_key_obj_id='" . $obj_id
          . "' AND objp_key_lang_id='" . $lang_id . "'" );

      if ( $value == 1 )
          $this->ccdSQL->query( "INSERT INTO obj_index_public VALUES('" . $obj_id . "','" . $lang_id . "')" );

      if ( $recursive  )
      {
          $query="SELECT obj_id FROM obj_index WHERE obj_parent_obj_id='" . $obj_id . "'";
          $res=$this->ccdSQL->query( $query );

          while ( $row=$this->ccdSQL->fetch_array( $res ) )
          {
              $this->update_visibility( $row['obj_id'], $lang_id, $value, true );
          }
      }
    }

// -------------------------------------------------------------------------------------------------------------------

}
